a simple example, Why does this give an error?
var zipped = [[0,1,2]];
var extracted = {};
var i = 0;
extracted[zipped[i][0]] = { zipped[i][1]: zipped[i][2] }
>>>Uncaught SyntaxError: Unexpected token [(…)
when this is perfectly fine?
extracted[0] = { 1: 2 }
Because Javascript object literal syntax does not allow expressions in key parts. Keys are always literal. That's why you can write this:
{ foo: 'bar' }
And foo is not taken as a variable.
For variable keys, you always have to use this pattern:
var obj = {};
obj[varKey] = value;
That is invalid syntax. A number is syntactically allowed in an object literal. Arbitrary expressions are not.
Instead, you need to create the object, then set the attribute.
var obj = extracted[zipped[i][0]] = {};
obj[ zipped[i][1] ] = zipped[i][2];
Related
As mentioned in MDN, Property names must be strings.
For code,
var foo = {unique_prop: 1}, obj = {};
obj[foo] = 'value';
console.log(obj[foo]);
In MDN, it says,
In the SpiderMonkey JavaScript engine, this string would be "['object Object']".
How does the property(string literal) of object type obj is stored like?
Is the property stored as "['unique_prop 1']" ?
When you create a property on an object from a variable, JS engine calls toString() method on a passed value. The actual value for the key is decided from its type.
You can check this behaviour yourself:
var foo = {};
foo.toString = function() {
return "toString";
}
var bar = {};
bar[foo] = "prop value";
for (var k in bar) {
console.log(k);
}
Can someone explain what is happening in the code below? I'd expect toString to get called for either both foo and bar, or neither. How is literal object notation different from adding fields to an object after it is created?
function Obj(v) {
this.v = v;
};
Obj.prototype.toString= function() {
window.alert("to string called for " +
this.v);
return this.v.toString();
}
var foo = new Obj('foo');
var bar = new Obj('bar');
// toString is not called here.
var map = {foo : 'blah'};
// toString is called here.
map[bar] = "blah2";
Why do object literals not use toString() while adding to an existing object does use toString()?
http://jsfiddle.net/pByGJ/2/
The main reason that object literals don't evaluate the identifier to the left of the colon is so you're not force to quote all literal names (as you do in JSON).
Bracket notation forces you to quote property names, if you don't, it will be evaluated as a variable.
The reason toString() does get called in the second example is because bar has to be converted to a string to be used as a property name.
In your first example, you're just creating a literal object (that is the exactly the same as {"foo" : 'blah'}). So that is never using the variable foo
If you want to create an object using a variable name, you can't use literal object notation, you have to use [] which is what forces it to call toString()
Here's a function to create objects with variable names in one expression.
function obj(key, value /*, key, value, ... */) {
var obj = {};
for (var i = 0, ln = arguments.length ; i < ln; i+=2) {
obj[arguments[i]] = arguments[i+1];
}
return obj;
}
Clearer Example
The fact that your variable names and values are the same doesn't help understanding the problem. Let me suggest this code
var foo = new Obj('fooValue');
var bar = new Obj('barValue');
var map = {foo : 'blah'};
map[bar] = "blah2";
// You expect map to be {fooValue: 'blah', barValue: 'blah2'}
// But it's {foo: 'blah', barValue: 'blah2'}
To do what you need, use my obj function
// Almost as clear as literal notation ???
var map = obj(
foo, 'blah',
bar, 'blah2'
);
// map = {fooValue: 'blah', barValue: 'blah2'} Yay!!
keys in an object literal are taken as strings, not interpreted as variables. This:
var map = {foo : 'blah'};
is equivalent to this:
var map = {"foo" : 'blah'};
and this:
var map = {};
map["foo"] = "blah";
but is completely different than this:
var map = {};
map[foo] = "blah";
Is it possible to create get/set function for undefined properties,
like in PHP __get() and __set() ?
You can access JavaScript object properties values using array access notation, you can also create a new property at any time using this notation or regular assignment notation.
var myObject = {};
myObject.Name = "Luis";
alert(myObject.Name);
alert(myObject["Name"]);
myObject["Name"] = "Dany";
alert(myObject.Name);
You can do
function ClassName(arg) {
var v = arg;
this.getter = function {
return v;
};
this.setter = function(val) {
v = val;
};
}
when you use it
var cn = new ClassName('a');
cn.setter('b');
alert(cn.getter()); /* alerts value 'b' */
Note that this uses the Constructor Invocation Pattern. By convention, you need to declare the function/class name with capital letter to indicate that this function/class need to be declared with the 'new' keyword.
Hope this helps
I have separate javascript files to load custom objects; these objects are designed to extend some default parameters when the main function is read
they each look like
var str1= { path:'url1:, var1:5};
var str2= { path:'url2:, var1:3};
etc...
I have an array of strings(that is generated from loading an rss page) and i want to return the object based on if its name matches the object name. hardcoding it would kind of defeat the purpose
Take a look at this question. It shows how to reference objects by using strings. The only difference I can see is that rather than starting with the window object, you would start at whatever object defines your current scope (e.g. this).
You can use the square bracket notation to refer to object properties whose key name is held in a variable:
var key = 'path';
var str1 = { path: 'url', var1: 1};
var value = str1[key]; // value == 'url';
You can also do:
var str1 = 'a';
var str2 = 'b';
var key = 'str1';
var value = window[key]; // value == 'a';
var map = { "string1": { path: "url1", var1:5},
"string2": { path: "url2", var1:3} };
...
var url = map[yourstring].path;
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
Is it at all possible to use variable names in object literal properties for object creation?
Example
function createJSON (propertyName){
return { propertyName : "Value"};
}
var myObject = createJSON("myProperty");
console.log(myObject.propertyName); // Prints "value"
console.log(myObject.myProperty); // This property does not exist
If you want to use a variable for a property name, you can use Computed Property Names. Place the variable name between square brackets:
var foo = "bar";
var ob = { [foo]: "something" }; // ob.bar === "something"
If you want Internet Explorer support you will need to use the ES5 approach (which you could get by writing modern syntax (as above) and then applying Babel):
Create the object first, and then add the property using square bracket notation.
var foo = "bar";
var ob = {};
ob[foo] = "something"; // === ob.bar = "something"
If you wanted to programatically create JSON, you would have to serialize the object to a string conforming to the JSON format. e.g. with the JSON.stringify method.
ES6 introduces computed property names, which allow you to do
function CreateJSON (propertyName){
var myObject = { [propertyName] : "Value"};
}
Note browser support is currently negligible.
You can sort of do this:
var myObject = {};
CreateProp("myProperty","MyValue");
function CreateProp(propertyName, propertyValue)
{
myObject[propertyName] = propertyValue;
alert(myObject[propertyName]); // prints "MyValue"
};
I much perfer this syntax myself though:
function jsonObject()
{
};
var myNoteObject = new jsonObject();
function SaveJsonObject()
{
myNoteObject.Control = new jsonObject();
myNoteObject.Control.Field1= "Fred";
myNoteObject.Control.Field2= "Wilma";
myNoteObject.Control.Field3= "Flintstone";
myNoteObject.Control.Id= "1234";
myNoteObject.Other= new jsonObject();
myNoteObject.Other.One="myone";
};
Then you can use the following:
SaveJsonObject();
var myNoteJSON = JSON.stringify(myNoteObject);
NOTE: This makes use of the json2.js from here:http://www.json.org/js.html
One thing that may be suitable (now that JSON functionality is common to newer browsers, and json2.js is a perfectly valid fallback), is to construct a JSON string and then parse it.
function func(prop, val) {
var jsonStr = '{"'+prop+'":'+val+'}';
return JSON.parse(jsonStr);
}
var testa = func("init", 1);
console.log(testa.init);//1
Just keep in mind, JSON property names need to be enclosed in double quotes.