问题描述
究竟什么是Javascript中的有效属性名称?各种财产分配方法有何不同?属性名称如何影响属性访问?
What, exactly, qualifies as a valid property name in Javascript? How do various methods of property assignment differ? And how does the property name affect property access?
我原始问题的答案(见下文) )帮助清除了一些事情,但也开启了新的蠕虫病毒。现在我已经有机会对JavaScript更加熟悉,我相信我已经能够想出很多。
The answers to my original question (seen below) helped to clear some things up, but also opened a new can of worms. Now that I've had a chance to become a bit more familiar with JavaScript, I believe I've been able to figure a lot of it out.
由于我很难将这些信息合并到一个解释中,我认为扩展原始问题可能会有所帮助,并试图回答它。
Since I had a hard time finding this information consolidated into one explanation, I thought it might be helpful to expand my original question, and attempt to answer it.
最初,与。具体来说,我想知道为什么他们声称如果属性名称不是有效的JavaScript标识符,那么它必须用引号括起来。然而,他们提供了示例代码,表明可以使用数字7 - 没有引号 - 作为属性名称。
Originally, there was some confusion with the MDN JavaScript guide (object literals). Specifically, I wondered why they claimed that if a property name was not a valid JavaScript identifier, then it would have to be enclosed in quotes. Yet, they offered example code that showed that the number 7 could be used — without quotes — as a property name.
事实证明,指南只留下了一个重要的是,对其进行了更新(更改)以粗体显示):
As it turns out, the guide simply left off one important part, and Pointy updated it (changes in bold):
我也想知道为什么允许属性名称偏离可能不以数字开头的规则,适用于标识符。这个问题实际上揭示了我对属性名称的完全误解,并且是导致我做更多研究的原因。
I also wondered why property names were allowed to deviate away from the "may not start with a digit" rule, that applies to identifiers. That question actually reveals the complete misunderstanding that I had of property names, and is what lead me to do some more research.
推荐答案
简答
对象属性名称可以是任何,,或(包括空字符串)。
Short Answer
Object property names can be any valid identifier, numeric literal, or string literal (including the empty string).
With也就是说,有一些可能令人困惑的复杂问题需要牢记JavaScript属性名称(摘要如下,)。
With that said, there are some potentially confusing intricacies to keep in mind about JavaScript property names (the summary is given below, and explored more on my own).
什么可能看起来像负数实际上是一个表达 - 所以不支持确定属性名称。
What might look like a negative number is actually an expression — something property names do not support.
var obj = {
-12: 'nope' // I am an invalid property name, because I am an expression.
};
幸运的是,括号表示法为我们处理表达式。
Fortunately, bracket notation handles expressions for us.
obj[-6] = 'yup'; // Successful property assignment. We're good here.
解析&类型转换
所有属性名称在存储之前都被转换为字符串。
Parsing & Typecasting
All property names are typecasted into strings before being stored.
var obj = {
lego: 'Everything is cool when you\'re part of a string.'
};
for (var key in obj) console.log(key); // "lego"
但在此之前,它们会根据所使用的语法进行解析,并进行转换到十进制文字。
But even before that, they are parsed according to the syntax used, and transformed into a decimal literal.
var objValid = {
'022': 'alligator', // valid string literal
6: 'cow', // interpreted as decimal, evaluates to 6
.345: 'bear', // interpreted as floating-point; evaluates to 0.345
1.000: 'fox', // interpreted as floating-point, evaluates to 1
8.9890: 'owl', // interpreted as floating-point, evaluates to 8.989
000888: 'fish', // interpreted as decimal, evaluates to 888
0777: 'monkey', // interpreted as octal, evaluates to 511
0x00111: 'elephant', // interpreted as hexadecimal, evaluates to 273
0b0011: 'donkey', // interpreted as binary, evaluates to 3
};
console.log(objValid['022']); // "alligator"; as expected
console.log(objValid[022]); // undefined; 022 is an octal literal that evaluates to 18 before our lookup ever occurs
console.log(objValid[6]); // "cow"; as expected
console.log(objValid['6']); // "cow"; as expected
console.log(objValid[0777]); // "monkey"; we're accessing the property name as it was assigned (before it was parsed and typecasted)
console.log(objValid['0777']); // undefined; after parsing and typecasting, our property name seems to have disappeared
console.log(objValid['511']); // "monkey"; there it is, we found it using the evaluation of our original assignment
console.log(objValid[0x00111]); // "elephant"; we're accessing the property name as it was assigned (before it was parsed and typecasted)
console.log(objValid['0x00111']); // undefined; after parsing and typecasting, our property name seems to have disappeared
console.log(objValid['273']); // "elephant"; there it is, we found it using the evaluation of our original assignment
console.log(objValid[0b0011]); // "donkey"; we're accessing the property name as it was assigned (before it was parsed and typecasted)
console.log(objValid['0b0011']); // undefined; after parsing and typecasting, our property name seems to have disappeared
console.log(objValid['3']); // "donkey"; there it is, we found it using the evaluation of our original assignment
结论
除非你正在使用有效的()数组索引 - 显式地将所有数字属性名称指定为字符串。
Conclusion
Unless you're working with valid (non-negative integer) array indexes — explicitly assign all numerical property names as strings.
这篇关于JavaScript中的有效属性名称,属性赋值和访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!