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。(?) 任何試圖提供其餘值(?)將致使一個錯誤拋出。 從新配置一個屬性首先須要刪除該屬性。若是屬性沒有刪除,就如同從新配置前的嘗試。