1、数据属性
- [[Configurable]]:能否通过delete删除属性从而重新定义属性。默认为true.
- [[Enumerable]]能否通过for-in循环删除属性。默认为true.
- [[Writable]]能否修改属性。默认为true.
- [[Value]]:包含属性的数据值。默认为undefined。
日期:2014-05-16 浏览次数:20549 次
1、数据属性
var person = {} Object.defineProperty(person, "name", { writable : false, configurable : false, value:"zhangsan" }); person.name = "lisi"; Object.defineProperty(person, "name", { writable : true }); document.write("pserson.name: " + person.name);?2、访问器属性
var book = {_year:2003,edition:5}; Object.defineProperty(book,"year",{ get:function(){ return this._year; }, set:function(newValue){ this._year = newValue; this.edition +=newValue-2003; } }); book.year = 2014; document.write("book.edition: " + book.edition);?3、读取属性的特性使用Object.getOwnPropertyDescriptor方法。eg.
var book = { _year : 2003, edition : 5 }; Object.defineProperties(book, { _year : { writable : false, configurable : false }, year : { get : function() { return this._year; }, set : function(newValue) { this._year = newValue; this.edition += newValue - 2003; } } }); book.year = 2014; var descriptor = Object.getOwnPropertyDescriptor(book, "_year"); console.log("descriptor: ",descriptor); document.write("book.edition: " + book.edition);?
?