Object.defineProperty(obj, prop, descriptor)数组
obj浏览器
propapp
descriptorless
返回传入函数的对象,即第一个参数objide
该方法容许精确添加或修改对象的属性。通常状况下,咱们为对象添加属性是经过赋值来建立并显示在属性枚举中(for...in 或 Object.keys 方法), 但这种方式添加的属性值能够被改变,也能够被删除。而使用 Object.defineProperty() 则容许改变这些额外细节的默认设置。例如,默认状况下,使用 Object.defineProperty() 增长的属性值是不可改变的。函数
对象里目前存在的属性描述符有两种主要形式:数据描述符和存取描述符。数据描述符是一个拥有可写或不可写值的属性。存取描述符是由一对 getter-setter 函数功能来描述的属性。描述符必须是两种形式之一;不能同时是二者。性能
数据描述符和存取描述符均具备如下可选键值:this
configurableprototype
enumerablecode
数据描述符同时具备如下可选键值:
value
writable
存取描述符同时具备如下可选键值:
get
set
记住,这些选项不必定是自身属性,若是是继承来的也要考虑。为了确认保留这些默认值,你可能要在这以前冻结 Object.prototype,明确指定全部的选项,或者将__proto__属性指向null。
// 使用 __proto__ Object.defineProperty(obj, "key", { __proto__: null, // 没有继承的属性 value: "static" // 没有 enumerable // 没有 configurable // 没有 writable // 做为默认值 }); // 显式 Object.defineProperty(obj, "key", { enumerable: false, configurable: false, writable: false, value: "static" }); // 循环使用同一对象 function withValue(value) { var d = withValue.d || ( withValue.d = { enumerable: false, writable: false, configurable: false, value: null } ); d.value = value; return d; } // ... 而且 ... Object.defineProperty(obj, "key", withValue("static")); // 若是 freeze 可用, 防止代码添加或删除对象原型的属性 // (value, get, set, enumerable, writable, configurable) (Object.freeze||Object)(Object.prototype);
若是你想知道如何用 binary-flags-like 语法使用 Object.defineProperty 方法,看看这篇文章。
若是对象中不存在指定的属性,Object.defineProperty()就建立这个属性。当描述符中省略某些字段时,这些字段将使用它们的默认值。拥有布尔值的字段的默认值都是false。value,get和set字段的默认值为undefined。定义属性时若是没有get/set/value/writable,那它被归类为数据描述符。
var o = {}; // 建立一个新对象 // Example of an object property added with defineProperty with a data property descriptor Object.defineProperty(o, "a", {value : 37, writable : true, enumerable : true, configurable : true}); // 对象o拥有了属性a,值为37 // Example of an object property added with defineProperty with an accessor property descriptor var bValue; Object.defineProperty(o, "b", {get : function(){ return bValue; }, set : function(newValue){ bValue = newValue; }, enumerable : true, configurable : true}); o.b = 38; // 对象o拥有了属性b,值为38 // The value of o.b is now always identical to bValue, unless o.b is redefined // 数据描述符和存取描述符不能混合使用 Object.defineProperty(o, "conflict", { value: 0x9f91102, get: function() { return 0xdeadbeef; } }); // throws a TypeError: value appears only in data descriptors, get appears only in accessor descriptors
若是属性已经存在,Object.defineProperty()将尝试根据描述符中的值以及对象当前的配置来修改这个属性。若是描述符的 configurable 特性为false(即该特性为non-configurable),那么除了 writable 外,其余特性都不能被修改,而且数据和存取描述符也不能相互切换。
若是一个属性的 configurable 为 false,则其 writable 特性也只能修改成 false。
若是尝试修改 non-configurable 属性特性(除 writable 之外),将会产生一个TypeError 异常,除非当前值与修改值相同。
当属性特性(property attribute) writable 设置为false时,表示 non-writable,属性不能被修改。
var o = {}; // 建立一个新对象 Object.defineProperty(o, "a", { value : 37, writable : false }); console.log(o.a); // 打印 37 o.a = 25; // 没有错误抛出(在严格模式下会抛出,即便以前已经有相同的值) console.log(o.a); // 打印 37, 赋值不起做用。
正如上例中看到的,修改一个 non-writable 的属性不会改变属性的值,同时也不会报异常。
属性特性 enumerable 定义了对象的属性是否能够在 for...in 循环和 Object.keys() 中被枚举。
var o = {}; Object.defineProperty(o, "a", { value : 1, enumerable:true }); Object.defineProperty(o, "b", { value : 2, enumerable:false }); Object.defineProperty(o, "c", { value : 3 }); // enumerable defaults to false o.d = 4; // 若是使用直接赋值的方式建立对象的属性,则这个属性的enumerable为true for (var i in o) { console.log(i); } // 打印 'a' 和 'd' (in undefined order) Object.keys(o); // ["a", "d"] o.propertyIsEnumerable('a'); // true o.propertyIsEnumerable('b'); // false o.propertyIsEnumerable('c'); // false
configurable 特性表示对象的属性是否能够被删除,以及除 writable 特性外的其余特性是否能够被修改。
var o = {}; Object.defineProperty(o, "a", { get : function(){return 1;}, configurable : false } ); // throws a TypeError Object.defineProperty(o, "a", {configurable : true}); // throws a TypeError Object.defineProperty(o, "a", {enumerable : true}); // throws a TypeError (set was undefined previously) Object.defineProperty(o, "a", {set : function(){}}); // throws a TypeError (even though the new get does exactly the same thing) Object.defineProperty(o, "a", {get : function(){return 1;}}); // throws a TypeError Object.defineProperty(o, "a", {value : 12}); console.log(o.a); // logs 1 delete o.a; // Nothing happens console.log(o.a); // logs 1
若是 o.a 的 configurable 特性已经为 true,没有错误会被抛出,而且属性会在最后被删除。
考虑特性被赋予的默认特性值很是重要,一般,使用点运算符和Object.defineProperty()为对象的属性赋值时,数据描述符中的属性默认值是不一样的,以下例所示。
var o = {}; o.a = 1; // 等同于 : Object.defineProperty(o, "a", { value : 1, writable : true, configurable : true, enumerable : true }); // 另外一方面, Object.defineProperty(o, "a", { value : 1 }); // 等同于 : Object.defineProperty(o, "a", { value : 1, writable : false, configurable : false, enumerable : false });
下面的例子说明了如何实现自我存档的对象。当 temperature 属性设置时,archive 数组会获得一个 log。
function Archiver() { var temperature = null; var archive = []; Object.defineProperty(this, 'temperature', { get: function() { console.log('get!'); return temperature; }, set: function(value) { temperature = value; archive.push({ val: temperature }); } }); this.getArchive = function() { return archive; }; } var arc = new Archiver(); arc.temperature; // 'get!' arc.temperature = 11; arc.temperature = 13; arc.getArchive(); // [{ val: 11 }, { val: 13 }]
另外一个例子:
var pattern = { get: function () { return 'I alway return this string,whatever you have assigned'; }, set: function () { this.myname = 'this is my name string'; } }; function TestDefineSetAndGet() { Object.defineProperty(this, 'myproperty', pattern); } var instance = new TestDefineSetAndGet(); instance.myproperty = 'test'; // 'I alway return this string,whatever you have assigned' console.log(instance.myproperty); // 'this is my name string' console.log(instance.myname);
规范版本 | 规范状态 | 说明 |
---|---|---|
ECMAScript 5.1 (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.8.5 |
ECMAScript 2015 (6th Edition, ECMA-262) | Standard | --- |
ECMAScript 2017 Draft (ECMA-262) | Draft | --- |
Desktop
特性 | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
基本支持 | 4.0 (2) | 5 | 9 [1] | 11.60 | 5.1 [2] |
Mobile
特性 | Firefox (Gecko) | Android | IE | Opera | Safari |
---|---|---|---|---|---|
基本支持 | 4.0 (2) | (Yes) | 9 | 11.5 | (Yes) |
[1] 在IE8中只支持 DOM 对象,同时也存在一些非标准的行为。
[2] Safari 5中也支持,但不能是 DOM 对象。
#兼容性问题
数组的 length 属性重定义是可能的,可是会受到通常的重定义限制。(length 属性初始为 non-configurable,non-enumerable 以及 writable。对于一个内容不变的数组,改变其 length 属性的值或者使它变为 non-writable 是可能的。可是改变其可枚举性和可配置性或者当它是 non-writable 时尝试改变它的值或是可写性,这二者都是不容许的。)然而,并非全部的浏览器都容许 Array.length 的重定义。
在 Firefox 4 至 22 版本中尝试去重定义数组的 length 属性都会抛出一个 TypeError 异常。
有些版本的Chrome中,Object.defineProperty() 在某些状况下会忽略不一样于数组当前length属性的length值。有些状况下改变可写性并不起做用(也不抛出异常)。同时,好比Array.prototype.push的一些数组操做方法也不会考虑不可读的length属性。
有些版本的Safari中,Object.defineProperty() 在某些状况下会忽略不一样于数组当前length属性的length值。尝试改变可写性的操做会正常执行而不抛出错误,但事实上并未改变属性的可写性。
只在Internet Explorer 9及之后版本和Firefox 23及之后版本中,才完整地正确地支持数组length属性的从新定义。目前不要依赖于重定义数组length 属性可以起做用,或在特定情形下起做用。与此同时,即便你可以依赖于它,你也没有合适的理由这样作。
Internet Explorer 8 实现了 Object.defineProperty() 方法,但 只能在 DOM 对象上使用。 须要注意的一些事情:
尝试在原生对象上使用 Object.defineProperty()会报错。 属性特性必须设置一些值。数据描述符为 true, true, true,configurable 为 true,enumerable 和 accessor 描述符为 false。(?) 任何试图提供其余值(?)将致使一个错误抛出。 从新配置一个属性首先须要删除该属性。若是属性没有删除,就如同从新配置前的尝试。