'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>object - Property names in JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

object - Property names in JavaScript - Stack Overflow

programmeradmin0浏览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. 暂无评论