Object.defineProperty()
方法會直接在一個對象上定義一個新屬性,或者修改一個對象的現有屬性, 並返回這個對象。數組
Object.defineProperty(obj, prop, descriptor)
被傳遞給函數的對象。app
該方法容許精確添加或修改對象的屬性。通常狀況下,咱們爲對象添加屬性是經過賦值來建立並顯示在屬性枚舉中(for...in
或 Object.keys
方法), 但這種方式添加的屬性值能夠被改變,也能夠被刪除。而使用 Object.defineProperty()
則容許改變這些額外細節的默認設置。例如,默認狀況下,使用 Object.defineProperty()
增長的屬性值是不可改變的。函數
對象裏目前存在的屬性描述符有兩種主要形式:數據描述符和存取描述符。數據描述符是一個擁有可寫或不可寫值的屬性。存取描述符是由一對 getter-setter 函數功能來描述的屬性。描述符必須是兩種形式之一;不能同時是二者。this
數據描述符和存取描述符均具備如下可選鍵值:prototype
configurable
描述符
纔可以被改變,同時該屬性也能從對應的對象上被刪除。默認爲 false。enumerable
當且僅當該屬性的 enumerable 爲 true 時,該屬性纔可以出如今對象的枚舉屬性中。
默認爲 false。 數據描述符同時具備如下可選鍵值:code
value
writable
當且僅當該屬性的 writable 爲 true 時,該屬性才能被賦值運算符改變。
默認爲 false。存取描述符同時具備如下可選鍵值:對象
get
undefined
。該方法返回值被用做屬性值。默認爲 undefined。 set
undefined
。該方法將接受惟一參數,並將該參數的新值分配給該屬性。默認爲 undefined。記住,這些選項不必定是自身屬性,若是是繼承來的也要考慮。爲了確認保留這些默認值,你可能要在這以前凍結 Object.prototype
,明確指定全部的選項,或者將_proto__
屬性指向null
。繼承
// 使用 __proto__ var obj = {}; var descriptor = Object.create(null); // 沒有繼承的屬性 // 默認沒有enumerable,沒有 configurable,沒有 writable descriptor.value = 'static'; Object.defineProperty(obj, 'key', descriptor); // 顯式 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 方法,看看這篇文章。ip
若是對象中不存在指定的屬性,Object.defineProperty()
就建立這個屬性。當描述符中省略某些字段時,這些字段將使用它們的默認值。擁有布爾值的字段的默認值都是false
。value
,get
和set
字段的默認值爲undefined
。定義屬性時若是沒有get/set/value/writable
,那它被歸類爲數據描述符。get
var o = {}; // 建立一個新對象 // 在對象中添加一個屬性與數據描述符的示例 Object.defineProperty(o, "a", { value : 37, writable : true, enumerable : true, configurable : true }); // 對象o擁有了屬性a,值爲37 // 在對象中添加一個屬性與存取描述符的示例 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 // o.b的值如今老是與bValue相同,除非從新定義o.b // 數據描述符和存取描述符不能混合使用 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);