1.属性的4特性:通过Object.getOwnPropertyDescriptor访问该属性的特性描述
(1).普通数据属性:
//返回{value: 1, writable: true, enumerable: true, configurable: true} Object.getOwnPropertyDescriptor({x: 1}, "x");
(2). getter setter:
//返回{get: func, set: func, enumerable: true, configurable: true} Object.getOwnPropertyDescriptor({x: 1, set a(){}, get a(){}}, "a");
2.修改自有属性的特性:
Object.defineProperty(o, { value: 1, writable: true, enumerable: false, configurable: true});
(2).将属性修改为getter setter
Object.defineProperty()(o, "x", {get: function(){return 0;}});
Object.defineProperties({}, { x: {value: 1, writable: true}, r: {get: function() {return 0;}} });
Object.defineProperty(Object.prototype, "extend", { writable: true, enumerable: false, configurable: true, value: function(o) { var names = Object.getOwnPropertyNames(o); for (var i = 0; i < names.length; i++) { if (names[i] in this) continue; var desc = Object.getOwnPropertyDescriptor(o, names[i]); Object.defineProperty(this, names[i], desc); } } });