Object.create中的可枚举参数是什么? [英] What is the enumerable argument for in Object.create?
问题描述
您想要 Object.create 的用法是什么?将枚举
设置为 true
?
In what usages of Object.create do you want to set enumerable
to true
?
推荐答案
如果您希望在遍历所有对象属性时能够访问它,则该对象的属性应该是可枚举的。例如:
A property of an object should be enumerable if you want to be able to have access to it when you iterate through all the objects properties. Example:
var obj = {prop1: 'val1', prop2:'val2'};
for (var prop in obj){
console.log(prop, obj[prop]);
}
在这种实例化中,enumerable始终为真,这将为您提供的输出
In this type of instantiation, enumerable is always true, this will give you an output of:
prop1 val1
prop2 val2
如果您会像这样使用Object.create():
If you would have used Object.create() like so:
obj = Object.create({}, { prop1: { value: 'val1', enumerable: true}, prop2: { value: 'val2', enumerable: false} });
您的for循环只会访问prop1,而不会访问prop2。默认情况下,使用Object.create()将属性设置为enumerable = false。
your for loop would only access the prop1, not the prop2. Using Object.create() the properties are set with enumerable = false by default.
这篇关于Object.create中的可枚举参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!