南来的,北往的,注意注意,请注意,hasOwnProperty() and isPrototypeOf()都是Object.prototype下面的两个方法,二者的区别:
一、hasOwnProperty()
用来判断某个对象是否含有指定的自身属性
obj.hasOwnProperty('a')//实例obj是否含有属性a,有->true,else->false,但是这个方法会忽略从原型继承来的属性和继承于其他对象的属性。
栗子:
<span style="font-size:18px;">var a =new Object();
a.prop = 'haha';
a.hasOwnProperty('prop');//true
a.hasOwnProperty('toString');//false
function B(){
this.name = 'san';
}
a.prototype = B;
a.hasOwnProperty('name');//false</span>
二、isPrototypeOf()
检查一个对像是否存在另一个对象的原型链上
var o={};
function Person(){};
var p1 =new Person();
Person.prototype=o;
var p2 =new Person();//继承自o
console.log(o.isPrototypeOf(p1));//false
console.log(o.isPrototypeof(p2));//true