First, how to determine the properties of an object instance is present in the object or prototype object ------ 2019-06-03

First, the knowledge base:
. 1, hasOwnProperty () method can be used to detect properties of the object instance are used in the present example the object is present in the prototype object;

      person1.hasOwnProperty("name")  实例对象调用该方法,将属性名传入该方法,
      (1)属性是对象本身的,返回true,
      (2)属性存在于原型对象,返回false;

2, the operator in effect:
(1) the official explanation: returns true if the given property can be accessed by the operator in the object, regardless of whether the property is present in the prototype example.
(2) vernacular: Whenever an instance of the object has attributes, whether the object instance itself is present in the prototype object, returns true, this property can be said to be used to detect whether there is the object property instance;
(3) Usage : "name" in person1 property in an object instance;

Second, use both syntax present in the detected property is the prototype example

function hasPrototypeProperty(object, name){ 
    return !object.hasOwnProperty(name) && (name in object); 
}
hasOwnProperty() 方法返回true,说明属性不是存在于实例对象中,in操作符返回true,说明属性存在,方法返回true说明属性存在于原型对象中;

Reproduced in: https: //www.jianshu.com/p/b6d37c1bb9d7

Guess you like

Origin blog.csdn.net/weixin_34138521/article/details/91168852