JavaScript-计算属性-深层困惑 [英] JavaScript - Computed properties - deep confusion
问题描述
似乎我对JavaScript中的计算属性感到很困惑.
Seems I am quite confused by computed properties in JavaScript.
当我定义一个对象并将[d]
用作键(作为属性键/名称)时,该[d]
实际做什么?似乎对于某些值d
,它会计算s = d.toString()
并将该值s
用作属性键.
但是对于其他值d
(例如,当d
是符号时),它实际上使用该符号的值作为键.
When I define an object and I put [d]
as a key (as a property key/name) what does this [d]
actually do? Seems that for some values d
it calculates s = d.toString()
and uses that value s
as the property key.
But for other values d
(e.g. when d
is a symbol) it uses really the symbol's value as the key.
因此,[d]
的这种双重行为(作为语法构造)似乎令人困惑.有人可以深入解释它是如何工作的吗?
So this dual behavior of [d]
(as a syntax construct) seems confusing. Could someone explain in depth how this works?
还有其他特殊情况吗?还是只有d
是Symbol时,我们才有这种特殊行为?
Are there other special cases btw? Or is it just when d
is a Symbol when we have that special behavior?
回到基础知识:什么事物可以是对象属性的键/名称?它是只是字符串还是只是字符串和符号,还是还有其他附加内容...?
Back to the basics: what things can be keys/names of properties of an object? Is it just strings or just strings and symbols or is there also something additional... ?
示例:
var symbol = Symbol("test");
function Animal(name){
this.name = name;
}
Animal.prototype = {};
Animal.prototype.constructor = Animal;
function Dog(breed){
this.breed = breed;
this.name = "Dog";
this.s = symbol;
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
console.log("001");
var d = new Dog("Sharo");
for (let x in d){
console.log(x, ":", d[x]);
}
console.log("002");
d = new Object();
for (let x in d){
console.log(x, ":", d[x]);
}
console.log("003");
d = new Number(5);
for (let x in d){
console.log(x, ":", d[x]);
}
var d1 = {};
var d2 = {};
var d = new Dog("Sharo");
var m = {[d1] : 5, [d2] : 10, [d] : 20, z : 100, symbol: 2000, [symbol] : 5000};
console.log("============================");
console.log(m);
for (let x in m){
console.log(x, ":", m[x]);
}
console.log("============================");
推荐答案
由于似乎没有人愿意回答这个问题,我将根据上面的评论自己回答这个问题,由于这个原因,我不再感到困惑.
Since no one seems interested in answering this question I will answer it myself based on the comments which I got above, and due to which I am not confused anymore.
请注意,此处的答案基于 ES6 .我的意思是……谁知道JavaScript的未来还有什么呢:)
Note that this answer here is ES6 based. I mean... who knows what else the JavaScript future will hold :)
当我定义一个对象并将
[d]
用作键(作为属性键/名称)时,该[d]
实际做什么?似乎对于某些对象d
,它会计算s = d.toString()
并将该值s
用作属性键.但是对于其他对象d
(例如,当d
是Symbol时),它实际上使用Symbol的值作为键.
When I define an object and I put
[d]
as a key (as a property key/name) what does this[d]
actually do? Seems that for some objectsd
it calculatess = d.toString()
and uses that values
as the property key. But for other objectsd
(e.g. whend
is a Symbol) it uses really the Symbol's value as the key.
是的,这是正确的.当d为Symbol时,其值将直接使用.当d是Symbol以外的任何值时,其值将强制为字符串,并且该字符串用作属性名称/键.强制更像是String(d)
而不是d.toString()
.
Yes, that's correct. When d is a Symbol its value is used directly. When d is anything but Symbol its value is coerced to a string and that string is used as the property name/key. The coercion is more like String(d)
rather than d.toString()
.
因此,
[d]
的这种双重行为(作为语法构造)似乎令人困惑.有人可以深入解释它是如何工作的吗?
So this dual behavior of
[d]
(as a syntax construct) seems confusing. Could someone explain in depth how this works?
已经在上面进行了解释.
Already explained above.
还有其他特殊情况吗?还是只有
d
是Symbol时,我们才有这种特殊行为?
Are there other special cases btw? Or is it just when
d
is a Symbol when we have that special behavior?
没有其他特殊情况".从ES6开始,只有字符串和符号可以是属性键.
There are no other "special cases". As of ES6 only strings and symbols can be property keys.
回到基础知识:什么事物可以是对象属性的键/名称?它是只是字符串还是只是字符串和符号,还是还有其他附加内容...?
Back to the basics: what things can be keys/names of properties of an object? Is it just strings or just strings and symbols or is there also something additional... ?
正如已经说过的那样,从ES6开始,只有字符串和符号可以是属性键.
As already said, as of ES6 only strings and symbols can be property keys.
参考:
(1) https://developer.mozilla.org/zh-美国/docs/Web/JavaScript/Reference/Operators/Property_Accessors
属性名称是字符串或符号.任何其他值(包括数字)都被强制为字符串."
"Property names are string or Symbol. Any other value, including a number, is coerced to a string."
(2) https://www.ecma-international.org/ecma-262/6.0/#sec-propertykey
这篇关于JavaScript-计算属性-深层困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!