对象判断属性是否存在

Object.prototype.hobby = 'basketball'
const mySymbol =  Symbol('本对象上的可遍历symbol值')
const mySymbol2 =  Symbol('本对象上的不可遍历symbol值')
const mySymbol3 =  Symbol('Object.prototype定义的symbol值')
Object.prototype[mySymbol3] = 'i am a symbol3333'
const foo  = {
    name:'kobe',
    age:18,
    [mySymbol]:'i am a symbol',
    
}
Object.defineProperty(foo,'address',{
    configurable:true,
    enumerable:false,
    writable:true,
    value:'xxxxxxx'
})
Object.defineProperty(foo,mySymbol2,{
    configurable:true,
    enumerable:false,
    writable:true,
    value:'i am symbol2222222'
})


console.log(Object.getPrototypeOf(foo));
//  foo现在一共六个属性,
//  address属性不可枚举,hobby属性在原型上;mysymbol是可遍历的symbol值,mysymbol是不可遍历的symbol值;
console.log(foo.hasOwnProperty('name'));  // true
console.log(foo.hasOwnProperty('address'));  //true
console.log(foo.hasOwnProperty(mySymbol));  //true
console.log(foo.hasOwnProperty('hobby'));  //false
//担心hasOwnProperty被重写,可以用这个方法;console.log(Object.prototype.hasOwnProperty.call(foo,'name'))  //true



for (const key in foo) {
    console.log(key);  //name age hobby
}


console.log(Object.keys(foo)); //[ 'name', 'age' ]
console.log(Object.getOwnPropertyNames(foo));  //[ 'name', 'age', 'address' ]
console.log(Object.getOwnPropertySymbols(foo));  //[ Symbol(mySymbol), Symbol(mySymbol22222) ]
console.log(Object.getOwnPropertyDescriptors(foo)); 
                                                      
                                                        //     name: {
                                                        //       enumerable: true,。。。。。。。。。
                                                        //     },
                                                        //     age: { value: 18, writable: true, enumerable: true, configurable: true },
                                                        //     address: {
                                                        //       enumerable: false,。。。。。。。。
                                                        //     },
                                                        //     [Symbol(mySymbol)]: {
                                                        //       enumerable: true,。。。。。。。。
                                                        //     },
                                                        //     [Symbol(mySymbol22222)]: {
                                                        //       enumerable: false,
                                                        //     }
                                                        //   }



//IN 刚好相反:会遍历到可枚举的东西,但是会遍历原型, SYMBOL不可以遍历

//总结:这四个都不可以遍历原型;
//Object.keys() 会遍历可枚举的属性,不可遍历symbol,不会遍历原型;
//Object.prototype.hasOwnProperty会遍历到不可枚举的东西,但是不会遍历原型, SYMBOL可以遍历 这个方法和getOwnPropertyDescriptors很像,只不过这个只是单纯的判断是否存在,只是单纯的反回boolean; // Object.getOwnPropertyNames 可以遍历不可枚举的东西,但是不会遍历原型,不能symbol // Object.getOwnPropertySymbols 可以遍历不可枚举的symbol;不遍历原型;之遍历symbol; // Object.getOwnPropertyDescriptors 会遍历到不可枚举的东西,不会遍历原型, 遍历symbol 这个方法就是上面两个的合体;

 

posted @ 2022-06-11 12:42  Eric-Shen  阅读(71)  评论(0编辑  收藏  举报