枚举属性
可枚举属性
对象的私有属性给类上面新增扩展的属性私有属性和原型上新增的属性都是可枚举的;
不可枚举
对象或原型中天生自带的属性属于不可枚举属性
检测一个属性是否是自己的私有属性
hasOwnProperty用来检测一个属性是否是自己的私有属性,是就返回true,反之false实例.hasOwnproperty(被检测的值)hasOwnproperty在Object的原型上
let ary
= [1,2,3];
console
.log( ary
.hasOwnProperty(0))
console
.log( ary
.hasOwnProperty('push'))
----------------------------------------------------------------------------------
var obj
= {a
:1};
console
.log(obj
.hasOwnProperty("b"))
检测一个属性是否属于某个对象
in用来检测一个属性是否属于某个对象私有公有都返回true
var obj
= {a
:1};
console
.log("a" in obj
);
console
.log("hasOwnProperty" in obj
);
封装检测一个属性是否是对象的公有属性
先用in看一下是不是自己的属性,如果是再用hasOwnProperty检测一下是不是自己的私有属性,那剩下的就是公有属性了
function Fn(){
this.a
=100;
this.b
=200;
}
Fn
.prototype
.getX=function(){
}
Fn
.prototype
.y=function(){
}
Fn
.prototype
={
x
:1
}
var f
= new Fn;
----------------------------------------------------------------------------------
function hasPublicProperty(obj
,attr
){
return attr
in obj
&& !obj
.hasOwnProperty(attr
)
}
console
.log(hasPublicProperty(f
,"x"))
-----------------------------------------------------------------------------
let ary
= [1, 2, 3];
console
.log(ary
.hasOwnProperty(0))
console
.log(ary
.hasOwnProperty('push'))
Object
.hasPubProperty = function (value
) {
let ary
= ['number', 'string'];
如果有用includes检测就是
true,反之就是
false
if (!ary
.includes(typeof value
)) {
return false
}
let n
= value
in this;
let m
= this.hasOwnProperty(value
);
return n
&& !m
;
}
console
.log(ary
.hasPubProperty('push'))
console
.log(ary
.hasPubProperty(0))
console
.log(ary
.hasPubProperty([]))
console
.log(Array
.prototype
.hasPubProperty('push'))
console
.log(Object
.hasPubProperty('hasOwnProperty'));
console
.log(Object
.hasOwnProperty('hasOwnProperty'));