javascript之對象屬性的檢測

1. 用in 檢測 對象是否含有某個屬性(私有屬性和繼承屬性)
var obj = {
    name:'apple',
    price:'200'
};
console.log('name' in obj);     //true,name 是obj 的私有屬性
console.log('price' in obj);    //true,price 是obj的私有屬性
console.log('time' in obj);     //false, time 不是obj的私有屬性
console.log('toString' in obj); //true,toString 是obj的繼承屬性
2. hasOwnProperty()方法檢測給定的屬性是否是對象的私有屬性,對於繼承屬性返回false
obj.hasOwnProperty('name');            //true
obj.hasOwnProperty('time');            //false
obj.hasOwnProperty('toString');        //false
3. propertyIsEnumerbale() 檢測私有屬性,且該屬性是可枚舉的才返回true.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章