[英]javascript - dot vs bracket notation in enumeration
I do know there's some differences between dot and bracket notations but for this specific problem I am a bit confused why the dot notation wouldn't work and bracket does. 我确实知道点和括号之间存在一些区别,但是对于这个特定的问题,我有点困惑为什么点不起作用而括号不能起作用。
var rockSpearguns = {
Sharpshooter: {barbs: 2, weight: 10, heft: "overhand"},
Pokepistol: {barbs: 4, weight: 8, heft: "shoulder"},
Javelinjet: {barbs: 4, weight: 12, heft: "waist"},
Firefork: {barbs: 6, weight: 8, heft: "overhand"}
};
function listGuns (guns) {
for(var speargun in guns){
console.log("Behold! "+speargun+", with "+ guns[speargun].heft +" heft!");
}
}
the part I am a bit confused is guns[speargun].heft
this would work properly but if I do guns.speargun.heft
then it will be undefined. 我有点困惑的部分是
guns[speargun].heft
它可以正常工作,但是如果我执行guns.speargun.heft
那么它将是不确定的。
Since the properties in the rockSpearguns are all just one word shouldn't gun.speargun
still able to call out the properties too? 由于rockSpearguns中的属性都是一个单词,难道不应该
gun.speargun
还能调用这些属性吗?
I have thought a bit was the reason because now speargun
is a string that if putting into gun.speargun
it actually becomes something like gun."speargun"
because if using bracket notation we just do gun[speargun]
instead of using gun["speargun"]
because this will just make it a double quote which is wrong. 我曾想过一点原因,因为现在
speargun
是一个字符串,如果放入gun.speargun
它实际上会变成类似于gun."speargun"
原因,因为如果使用括号符号,我们只是使用gun[speargun]
而不是使用gun["speargun"]
因为这只会使它成为双引号,这是错误的。
Yeah is really ambiguous, there is a rules of thumb: 是的,确实很含糊,有一个经验法则:
use brackets when the property value to access is generated dynamically, example: 动态生成要访问的属性值时,请使用方括号,例如:
var Person = { name: 'John', lastName: 'Doe' }; var propertyToCheck = 'name'; console.log(Person.propertyToCheck); //error! console.log(Person[propertyToCheck]); //correct way
use dots when the property value to access is not generated dynamically(you know the property beforehand), example: 当要动态访问的属性值不是动态生成时(请事先知道该属性),请使用点,例如:
var Person = { name: 'John', lastName: 'Doe' }; console.log(Person.name);
By the way, you can use brackets in both cases, but I prefer to choose what I just mentioned above because using brackets seem you are working with arrays and not with objects 顺便说一句,您可以在两种情况下都使用方括号,但是我更喜欢选择上面刚刚提到的内容,因为使用方括号似乎是在处理数组而不是对象
Hope this helps you 希望这对您有帮助
The equivalent of 相当于
speargun = 'Sharpshooter';
guns[speargun].heft
is 是
guns['Sharpshooter'].heft
or 要么
guns.Sharpshooter.heft
because the variable in square brackets is evaluated and the content is inserted into the brackets. 因为计算方括号中的变量并将内容插入方括号中。 You get the second paragraph.
您将看到第二段。
If you have a string literal, then you can use it as property accessor for the object with dots. 如果您有字符串文字,则可以将其用作带点对象的属性访问器 。
In your above case, you use 在上述情况下,您使用
guns.speargun.heft
which does not exist, because you have no property speargun
in the object guns
. 这不存在,因为在对象
guns
没有属性speargun
。
It is not working because there is no speargun property in the rockSpearguns object. 它不起作用,因为rockSpearguns对象中没有speargun属性。
The reason is that in a JS object, all property keys are strings.
原因是在JS对象中,所有属性键都是字符串。 When you use dot notation, JS is thinking you are looking for a key with the explicit key of whatever is after the dot.
当您使用点表示法时,JS会认为您正在寻找一个带有显式键的键,该键与点后的内容相同。
In your code var speargun
is being replaced with a string value of each of the properties inside the rockSpearguns object. 在代码
var speargun
被替换与每个rockSpearguns对象内的属性的字符串值。
So, guns[speargun].heft
translates to guns["Sharpshooter"].heft
因此,
guns[speargun].heft
转换为guns["Sharpshooter"].heft
I suggest you to read this article . 我建议您阅读这篇文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.