为什么可以通过Object.defineProperty枚举ES6符号属性? [英] Why can ES6 Symbol properties be made enumerable by Object.defineProperty?
问题描述
var symbol = Symbol();
var object = {};
object [symbol] ='value';
MDN将枚举属性定义为可由for..in循环迭代的属性(1 )。符号属性永远不会被一个for ... in循环迭代,因此它们可以被认为是非枚举的(2)。
这样做是否有意义呢?你可以这样做:
Object.defineProperty(object,symbol,{
value:'value',
可枚举:true
});
该查询对象为其描述符确实确认此属性是可枚举的:
Object.getOwnPropertyDescriptor(object,symbol)
// - > {enumerable:true}
为什么?这是什么用?
(1) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
(2)for ... in ... [ [枚举]]
,其中只包含字符串键。可能,现在我们应该改变MDN上的定义,我们有符号属性。
是的,有一个允许符号
属性可枚举: Object.assign
:
let s1 = Symbol();
let s2 = Symbol();
let s3 = Symbol();
let original = {};
original [s1] =value1; // Enumerable
Object.defineProperty(original,s2,{// Enumerable
enumerable:true,
value:value2
});
Object.defineProperty(original,s3,{// Non-enumerable
value:value3
});
let copy = {};
Object.assign(copy,original);
console.log(copy [s1] is+ copy [s1]); // value1,因为它是可枚举的
console.log(copy [s2] is+ copy [s2]); // value2,因为它是可枚举的
console.log(copy [s3] is+ copy [s3]); //未定义,因为它不是可枚举的
为了清楚起见:
MDN将枚举属性定义为可由for..in循环迭代的属性(1)。
对于ES6(ES2015)来说这是错误的。在ES5和更早版本中,这是一个合理的,如果是简单化的定义,不会因为 Symbol
而不再简单地更正。我已经修复了这篇文章。
这是一个CW,因为它是对这个问题的评论的产物。 / p>
In ES6 properties can be defined as symbol properties:
var symbol = Symbol();
var object = {};
object[symbol] = 'value';
MDN defines enumerable properties as 'those which can be iterated by a for..in loop' (1). Symbol properties are never iterated by a for...in loop, therefore they can be considered non-enumerable (2).
Does it make any sense, then, that you can do this:
Object.defineProperty(object, symbol, {
value: 'value',
enumerable: true
});
and that querying object for it's descriptor does indeed confirm that this property is enumerable:
Object.getOwnPropertyDescriptor(object, symbol)
// -> { enumerable: true }
Why? What use is this?
(1) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
(2) for...in uses [[Enumerate]]
, which only includes string keys. Probably the definition on MDN should be changed now that we have symbol properties.
Yes, there's a reason for allowing Symbol
properties to be enumerable: Object.assign
:
let s1 = Symbol();
let s2 = Symbol();
let s3 = Symbol();
let original = {};
original[s1] = "value1"; // Enumerable
Object.defineProperty(original, s2, { // Enumerable
enumerable: true,
value: "value2"
});
Object.defineProperty(original, s3, { // Non-enumerable
value: "value3"
});
let copy = {};
Object.assign(copy, original);
console.log("copy[s1] is " + copy[s1]); // value1, because it was enumerable
console.log("copy[s2] is " + copy[s2]); // value2, because it was enumerable
console.log("copy[s3] is " + copy[s3]); // undefined, because it wasn't enumerable
Live Copy on Babel's REPL.
Just for clarity:
MDN defines enumerable properties as 'those which can be iterated by a for..in loop' (1).
That's simply wrong for ES6 (ES2015). It was a reasonable, if simplistic, definition in ES5 and earlier, no it's no longer even simplistically correct because of Symbol
s. I've fixed the article.
This is a CW because it was the outgrowth of the comments on the question.
这篇关于为什么可以通过Object.defineProperty枚举ES6符号属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!