我正在阅读有关ECMAScript 5的更多内容(在浏览器中,使用我知道不支持所有内容的ES5-shim)。考虑到我有这个对象(从this post被盗),只是为了消除任何困惑:
var Person = {
firstName: null, // the person’s first name
lastName: null // the person’s last name
};
这有什么区别:
var Employee = Object.create(Person, {
id: {
value: null,
enumerable: true,
configurable: true,
writable: true
}
});
而且:
var Employee = Object.create(Person);
Employee.id = null;
// Or using jQuery.extend
$.extend(Employee, {
id : null
});
据我所知,可枚举,可配置和可写在以这种方式定义属性时设置为true(它也可以向后兼容传统的JavaScript引擎)。我是否遗漏了某些内容,或者只要我希望这是一个理想的行为,我就可以省略详细的属性描述符?
答案 0 :(得分:4)
他们是一样的。
按作业创建新属性时
obj.prop = val;
新创建的属性的所有三个布尔属性都设置为true
。
另外,请注意通过Object.defineProperty
Object.defineProperty( obj, 'prop', {
value: val
});
布尔属性设置为false
(默认情况下)。