JavaScript中propertie和attribute的区别
问题描述
A property has a name and a value. A property name may be any string, including the empty string, but no object may have two properties with the same name. The value may be any JavaScript value, or (in ECMAScript 5) it may be a getter or a setter function (or both). In addition to its name and value, each property has associated values that we’ll call property attributes: writable attribute, enumerable attribute and configurable attribute
也就是说,对象的属性就是property,例如:
window.Infinity; // Inifinity是window对象的property
var obj = {x: 1, y: 2}; // x, y都是obj对象的property
但是ES5中额外提供了一些东西去描述对象属性(property)的属性(attribute),即是 只读的(writable),可枚举的(enumerable),和可配置的(configurable)。
这些attributes的定义方式如:
var o = {};
Object.defineProperty(o, "x", {
value : 1,
writable: true, // o对象x属性(property)的writable属性(attribute)
enumerable: false, // o对象x属性(property)的enumerable属性(attribute)
configurable: true // o对象x属性(property)的configurable属性(attribute)
});
另外,还有Object attributes:
In addition to its properties, every object has three associated object attributes: prototype, class, extensible
(以上定义来自犀牛书)
这篇关于JavaScript中propertie和attribute的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!