最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

object - Property names in JavaScript - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Mar 12, 2013 at 3:17 CodistCodist 1,2082 gold badges12 silver badges29 bronze badges 8
  • var bar = 'foobar'; can't work in your case? – Raptor Commented Mar 12, 2013 at 3:19
  • 2 That's the way the syntax works. – SLaks Commented Mar 12, 2013 at 3:20
  • obj['property'] syntax requires it. – Jared Farrish Commented Mar 12, 2013 at 3:21
  • 1 Your example doesn't have a numeric literal, and both the given examples are valid property names. I personally don't understand what you're after. – Phix Commented Mar 12, 2013 at 3:21
  • 1 I remend reading "JavaScript - the Good Parts". After a couple chapters you'll understand stuff like this pletely. – jahroy Commented Mar 12, 2013 at 3:36
 |  Show 3 more ments

4 Answers 4

Reset to default 8

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/ecma-262/5.1/#sec-11.1.5

So, for example:

var obj = {
    foo: true, // the name of this property bees 'foo'
    'bar': true, // the name of this property bees 'bar'
    123: true // the name of this property bees '123'
};

You can even use the empty string as a property name:

var obj = {
    '': 'foo'
};

obj[''] // 'foo'

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论