JS hasOwnProperty() method: Check whether a property is an object's own property

JavaScript hasOwnProperty() method is Object's prototype method (also called instance method), it is defined on Object.prototype object, all Object instance objects will inherit the hasOwnProperty() method.

The hasOwnProperty() method is used to detect whether a property is an object's own property, rather than inherited from the prototype chain. If the attribute is a self-owned attribute, then return true, otherwise return false. In other words, the hasOwnProperty() method does not detect the prototype chain of the object, only the current object itself, and returns true only when the current object itself has the property.

For example, in the custom type below, this.name represents the object's own property, and the name property in the prototype object is an inherited property.

function F() {
    
      //自定义数据类型
    this.name = "自有属性";
}
F.prototype.name = "继承属性";

The syntax format of hasOwnProperty() is as follows:

object.hasOwnProperty(propertyName);

Parameter description: The propertyName parameter represents the name of the property to be detected.

Return value: Return a Boolean value. If propertyName is its own property, then return true, otherwise return false.
Example 1 For
the above custom type, you can instantiate the object, and then determine the type of the attribute name called by the current object.

var f = new F();  //实例化对象
console.log(f.hasOwnProperty("name"));  //返回true,说明当前调用的 name是自有属性
console.log(f.name);  //返回字符串“自有属性”

All the prototype properties of the constructor (the properties contained in the prototype object) are inherited properties and will return false when tested with the hasOwnProperty() method. However, for the prototype object itself, these prototype properties are their own properties, so the return value is true again.
Example 2
In the following example, it is demonstrated that the toString() method is an inherited property for the Date object, but it is its own property for the prototype object of the Date constructor.

var d = Date;
console.log(d.hasOwnProperty("toString"));  //返回false,说明toString()是Date的自有属性
var d = Date.prototype;
console.log(d.hasOwnProperty("toString"));  //返回true,说明toString()是Date.prototype属性

The hasOwnProperty() method can only determine whether the specified object contains the property with the specified name, and cannot check whether the object prototype chain contains a certain property, so the property that can be detected must be an object member.
Example 3
The following example demonstrates the range of properties that the hasOwnProperty() method can detect.

var o = {
    
      //对象直接量
    o1 : {
    
      //子对象直接量
        o2 : {
    
      //孙子对象直接量
            name : 1  //孙子对象直接量的属性
        }
    }
};
console.log(o.hasOwnProperty("o1"));  //返回true,说明o1是o的自有属性
console.log(o.hasOwnProperty("o2"));  //返回false,说明o2不是o的自有属性
console.log(o.o1.hasOwnProperty("o2"));  //返回true,说明o2是o1的自有属性
console.log(o.o1.hasOwnProperty("name"));  //返回false,说明name不是o1的自有属性
console.log(o.o1.hasOwnProperty("name"));  //返回true,说明name不是o2的自有属性

Guess you like

Origin blog.csdn.net/qq_47008195/article/details/108699796