除了第一种形式可以使用变量而不仅仅是字符串文字这一显而易见的事实之外,是否有任何理由使用其中一种而不是另一种,如果是这样,在哪些情况下?
在代码:
// Given:
var foo = {'bar': 'baz'};
// Then
var x = foo['bar'];
// vs.
var x = foo.bar;
上下文:我写了一个代码生成器,产生这些表达式,我想知道哪个更可取。
除了第一种形式可以使用变量而不仅仅是字符串文字这一显而易见的事实之外,是否有任何理由使用其中一种而不是另一种,如果是这样,在哪些情况下?
在代码:
// Given:
var foo = {'bar': 'baz'};
// Then
var x = foo['bar'];
// vs.
var x = foo.bar;
上下文:我写了一个代码生成器,产生这些表达式,我想知道哪个更可取。
当前回答
当-时必须使用方括号
属性名是number。 Var ob = { 1:“一个”, 7:“7” } ob.7 // SyntaxError ob[7] // " 7 " 属性名有特殊字符。 Var ob = { 'This is one': 1, 这是7:7, } ob.'This is one' // SyntaxError ob['这是一个']// 1 属性名被赋给一个变量,您要访问 属性值。 Var ob = { “一”:1、 “七”:7, } var _Seven = ' 7 '; b. _seven //未定义 ob[_Seven] // 7 .使用实例
其他回答
使用这些符号时要小心: 如。如果我们想访问一个窗口的父函数。 在IE中:
window['parent']['func']
并不等同于
window.['parent.func']
我们可以用:
window['parent']['func']
or
window.parent.func
访问它
如果属性名有特殊字符,则需要使用括号:
var foo = {
"Hello, world!": true,
}
foo["Hello, world!"] = false;
除此之外,我想这只是个人喜好的问题。恕我直言,点表示法更短,而且更明显地表明它是一个属性而不是数组元素(当然JavaScript没有关联数组)。
The two most common ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the part after the dot must be a valid variable name, and it directly names the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.
So if you know that the property you are interested in is called “length”, you say value.length. If you want to extract the property named by the value held in the variable i, you say value[i]. And because property names can be any string, if you want to access a property named “2” or “John Doe”, you must use square brackets: value[2] or value["John Doe"]. This is the case even though you know the precise name of the property in advance, because neither “2” nor “John Doe” is a valid variable name and so cannot be accessed through dot notation.
对于数组
数组中的元素存储在属性中。因为这些属性的名称都是数字,而且我们经常需要从变量中获取它们的名称,所以我们必须使用括号语法来访问它们。数组的length属性告诉我们它包含多少个元素。这个属性名是一个有效的变量名,我们事先知道它的名字,所以要找到一个数组的长度,你通常写array。长度,因为它比数组[" Length "]更容易写。
(来源此处)
方括号表示法允许使用点表示法不能使用的字符:
var foo = myForm.foo[];//错误的语法 var foo = myForm["foo[]"];//正确的语法
包括非ascii (UTF-8)字符,如myForm["ダ"](更多示例)。
其次,方括号符号在处理时很有用 以可预测的方式变化的属性名:
For (var I = 0;I < 10;我+ +){ someFunction(myForm["myControlNumber" + i]); }
摘要:
点表示法写起来更快,读起来更清楚。 方括号符号允许访问包含 特殊字符的选择 使用变量的属性
不能与点表示法一起使用的字符的另一个例子是本身包含点的属性名。
例如,json响应可以包含一个名为bar.Baz的属性。
var foo = myResponse.bar.Baz; // incorrect syntax
var foo = myResponse["bar.Baz"]; // correct syntax
点表示法和括号表示法都用于在JavaScript中访问对象属性。点表示法是最常用的,因为它更容易阅读和理解。为什么我们要用括号,这两者有什么区别呢?好吧,方括号符号[]允许我们使用变量访问对象属性,因为它将方括号内的表达式转换为字符串。
Const person = { 名称:“约翰”, 年龄:30 }; / /点符号 const nameDot = person.name; console.log (nameDot); / /“约翰” const name括号= person['name']; console.log (nameBracket); / /“约翰”
现在,让我们看一个变量的例子:
Const person = { 名称:“约翰”, 年龄:30 }; const myName = 'name'; console.log(人[名字]); / /“约翰”
点表示法的另一个优点是只包含字母数字(以及_和$),例如,如果你想访问一个像下面这样的对象(包含'-',你必须使用括号表示法)
Const person = { my-name: John } console.log(人['我的名字']);/ /“约翰” / / console.log (person.my-name);/ /错误