hasOwnProperty
hasOwnProperty方法是用来判断某个属性是否是属于自身对象的,该方法仅用于判断自身对象,不会查找原型链。
案例如下:
Person.prototype.name = '原型'
Person.prototype.myFc = function(){}
function Person(){
this.age = 18
this.myFc = function(){}
}
var per = new Person()
console.log("name",per.name) //原型
console.log("是否是自己的?",per.hasOwnProperty('name')) //false
console.log("是否是自己的?",per.hasOwnProperty('age')) //true
console.log("是否是自己的?",per.hasOwnProperty('myFc')) //true
instanceof
instanceof方法是判断某个对象是否由某个构造函数构建。
如A instanceof B,判断A对象是否由B构造函数创建。
案例:
function Person(){}
var per = new Person()
var num = new Number()
console.log(per instanceof Person) //true
console.log(num instanceof Number) //true
console.log(per instanceof Object) //true
console.log(num instanceof Object) //true
一般来说,js中的对象最终都是Object构造出来的。
因此per instanceof Object,num instanceof Object的结果都是true.