javascript之对象属性的检测

来源:互联网 发布:护眼台灯推荐知乎 编辑:程序博客网 时间:2024/06/06 01:44
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');            //trueobj.hasOwnProperty('time');            //falseobj.hasOwnProperty('toString');        //false
3. propertyIsEnumerbale() 检测私有属性,且该属性是可枚举的才返回true.