Reputation: 1208
I have a question that has been really bugging me for quite a while and I cannot seem to find any resources that cover the topic. How can property names in JavaScript be string literals or numeric literals?
var obj = {
"bar": "foobar",
"foo": function() { return bar; }
}
This topic has bugged me ever since I learned about it a few years ago. I don't know where to get more in-depth information or what this is even called. I not confused on how this is set up as I know that a new object is being creating with members, bar & foo and then its assigned to the obj variable.
You cannot create a variable like var "bar" = "foobar"; because you'll get a syntax error. How is it valid for object literals? Any help on this would be very much appreciated.
Upvotes: 0
Views: 1956
Reputation: 191779
The object literal syntax you are using is just part of JavaScript's syntax. You can use numeric or string literal as a property name as well as any valid variable name as a property name. Note that invalid variable names must be wrapped in quotes, but can still be property names (numeric literals being an exception).
That is, you can have obj = {'"': value}
, i.e. a quote, as a valid object property name. However, if you left off the apostrophes there it would be a syntax error.
The variable name syntax, e.g. {nameWithoutQuotes: "value"}
is allowed, as far as I can tell, for convenience. It has no special meaning and is treated as if it were a string literal property name. It would look very odd to have "
everywhere in an object literal definition, and it also makes sense when using similar accessor syntax. For example:
obj = {"with quotes": "q", withoutQuotes: "x"};
obj["with quotes"];
obj.withoutQuotes;
Note that the method of access with a property name that requires quotes also requires quotes whereas when quotes are not required access can be done without them.
As for why "obj" = "string"
is not allowed, other than the fact that it is invalid syntax, that is because the "obj"
literal does not create a reference in memory that can be assigned to. The obj = {}
notation creates a reference that is stored in obj
and memory is allocated for each of its properties as described by the literal syntax. You could make a similar statement about obj = "string";
It may also be worth nothing that the quotes cannot be omitted from a JSON string for property names. Many parsers will not allow it.
Upvotes: 2
Reputation: 185963
In JavaScript, property names are String values - any String values. That's just how the language is specified.
The relevant production:
PropertyName :
IdentifierName
StringLiteral
NumericLiteral
If an identifier, or a numeric literal is supplied, it is converted to a string value (SV).
See: http://ecma-international.org/ecma-262/5.1/#sec-11.1.5
So, for example:
var obj = {
foo: true, // the name of this property becomes 'foo'
'bar': true, // the name of this property becomes 'bar'
123: true // the name of this property becomes '123'
};
You can even use the empty string as a property name:
var obj = {
'': 'foo'
};
obj[''] // 'foo'
Upvotes: 8
Reputation: 17589
In order to see that it is absolutely valid you just have to recall that objects are associative arrays , ie
foo.bar === foo['bar']
In this case keys of associative array are any strings.
Upvotes: 1
Reputation: 12123
JavaScript object literals are hashmap implementations: i.e., key-value pairs. The keys can be represented either in quotes or without quotes.
That said, if you want to access a property as a string, you use the syntax below:
obj[str]
But if you want to access a property by its name, you use
obj.name
Upvotes: 2