預約義以下對象:prototype
Object.prototype.parentKey = 'pValue'; Object.prototype.parentFun = function(){}; let obj = { mykey: 'value', myFun: function(){}, [Symbol()]:'symbolValue' } // 在obj上定義2個不可枚舉的屬性 Object.defineProperty(obj, 'myUnemurableKey', {value:'value22', enumerable:false}); Object.defineProperty(obj, 'myUnenmurableFunc', {value:function(){}, enumrable:false});
用於遍歷自身和原型對象的可枚舉的屬性code
for (let key in obj) { console.log(key) }
輸出:
mykey
myFun
parentKey
parentFun對象
用於返回對象自身的全部可枚舉屬性ip
Object.keys(obj)
輸出:
["mykey", "myFun"]get
用於返回對象全部屬性,包含可枚舉屬性和不可枚舉屬性,不包含Symbol屬性原型
Object.getOwnPropertyNames(obj)
輸出:
["mykey", "myFun", "myUnemurableKey", "myUnenmurableFunc"]it
用於返回對象全部屬性,包含可枚舉屬性和不可枚舉屬性,只包含Symbol屬性io
Object.getOwnPropertySymbols(obj)
輸出:
[Symbol()]console
用於返回對象全部屬性,包含可枚舉屬性和不可枚舉屬性,也包含Symbol屬性table
Reflect.ownKeys(obj)
輸出:
["mykey", "myFun", "myUnemurableKey", "myUnenmurableFunc", Symbol()]
這個方法能夠返回對象自身全部屬性的描述對象,包含key, value, writable, enumerable, configurable。
Object.getOwnPropertyDescriptors(obj)
輸出:
{ "mykey": { "value": "value", "writable": true, "enumerable": true, "configurable": true }, "myFun": { "writable": true, "enumerable": true, "configurable": true }, "myUnemurableKey": { "value": "value22", "writable": false, "enumerable": false, "configurable": false }, "myUnenmurableFunc": { "writable": false, "enumerable": false, "configurable": false } }