【JavaScript】属性名の遍歴の方法別forin、Object.keys、OwnProperty、Reflect.ownKeys
6483 ワード
var obj = {
name:'kkk',
age:88,
is: true,
99:100,
[Symbol()]:"symbol"
}
Object.prototype.addone = "one" // obj addone
Object.defineProperty(obj,"name",{
enumerable: false }) // obj name
for (var item in obj){ console.log(item)} // 99,age,is,addone
for (var item in Object) {console.log(item)} //undefined Object
Object.keys(obj)
//(3) ["99", "age", "is"] addone , symbol,name
Object.keys(Object)
//[]
Object.getOwnPropertyNames(obj)
//(4) ["99", "name", "age", "is"]
Object.getOwnPropertyNames(Object)
//(24) ["length", "name", "prototype", "assign", "getOwnPropertyDescriptor", "getOwnPropertyDescriptors", "getOwnPropertyNames", "getOwnPropertySymbols", "is", "preventExtensions", "seal", "create", "defineProperties", "defineProperty", "freeze", "getPrototypeOf", "setPrototypeOf", "isExtensible", "isFrozen", "isSealed", "keys", "entries", "values", "fromEntries"]
Object.getOwnPropertySymbols(obj)
//[Symbol()]
Object.getOwnPropertySymbols(Object)
//[]
Reflect.ownKeys(obj)
//(5) ["99", "name", "age", "is", Symbol()]
Reflect.ownKeys(Object)
//(24) ["length", "name", "prototype", "assign", "getOwnPropertyDescriptor", "getOwnPropertyDescriptors", "getOwnPropertyNames", "getOwnPropertySymbols", "is", "preventExtensions", "seal", "create", "defineProperties", "defineProperty", "freeze", "getPrototypeOf", "setPrototypeOf", "isExtensible", "isFrozen", "isSealed", "keys", "entries", "values", "fromEntries"]