The JS hasOwnProperty of method detects whether an attribute is an object's own attribute

  • 2021-10-25 05:51:10
  • OfStack

The JavaScript hasOwnProperty () method is a prototype method (also called an instance method) for Object, which is defined on top of the Object. prototype object, and all Object instance objects inherit the hasOwnProperty () method.

The hasOwnProperty () method is used to detect whether an attribute is owned by the object and not inherited from the prototype chain. Returns true if the property is owned, or false if not. In other words, the hasOwnProperty () method does not detect the prototype chain of the object, only the current object itself, and returns true only if the attribute exists in the current object itself.

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

function F() { // Custom data type = " Own attribute ";
} = " Inheritance attribute ";

The syntax format of hasOwnProperty () is as follows:


Parameter description: The propertyName parameter indicates the name of the attribute to be detected.

Return value: Returns 1 Boolean value. If propertyName is owned, true is returned, otherwise false is returned.

Example 1

For the custom type above, you can instantiate the object and determine what type of property name is being called by the current object.

var f = new F(); // Instantiate object 
console.log(f.hasOwnProperty("name")); // Return true That describes the currently invoked  name Is its own attribute 
console.log(; // Returns the string "Own Properties" 

Any stereotype property of a constructor (the property contained in a stereotype object) is an inherited property and returns false when detected using the hasOwnProperty () method. However, for the prototype object itself, these prototype attributes are its own attributes, so the return value is true.

Example 2

In the following example, we demonstrate that the toString () method is an inherited property for the Date object, but its own property for the prototype object of the Date constructor.

var d = Date;
console.log(d.hasOwnProperty("toString")); // Return false , description toString() Yes Date Own attributes of 
var d = Date.prototype;
console.log(d.hasOwnProperty("toString")); // Return true , description toString() Yes Date.prototype Attribute 

The hasOwnProperty () method can only determine whether the specified object contains an attribute with the specified name, but cannot check whether the object prototype chain contains an attribute, so the attribute 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 = { // Object direct quantity 
  o1 : { // Subobject direct quantity 
    o2 : { // Direct quantity of grandson object 
      name : 1 // Attributes of direct quantities of grandchildren objects 
console.log(o.hasOwnProperty("o1")); // Return true , description o1 Yes o Own attributes of 
console.log(o.hasOwnProperty("o2")); // Return false , description o2 No o Own attributes of 
console.log(o.o1.hasOwnProperty("o2")); // Return true , description o2 Yes o1 Own attributes of 
console.log(o.o1.hasOwnProperty("name")); // Return false , description name No o1 Own attributes of 
console.log(o.o1.hasOwnProperty("name")); // Return true , description name No o2 Own attributes of 

Related articles: