The JS hasOwnProperty() method detects whether a property is an object's own property.

The JS hasOwnProperty() method detects whether a property is an object's own property.

The JavaScript hasOwnProperty() method is the prototype method (also called instance method) of Object. It is defined on the Object.prototype object. All instance objects of Object 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. Returns true if the property is an owned property, false otherwise. In other words, the hasOwnProperty() method does not detect the prototype chain of the object, but only detects the current object itself, and returns true only when the property 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 the inherited property.

function F() { //Custom data type this.name = "own property";
}
F.prototype.name = "Inherited properties";

The syntax of hasOwnProperty() is as follows:

object.hasOwnProperty(propertyName);

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

Return value: Returns a Boolean value. Returns true if propertyName is an owned property, false otherwise.

Example 1

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

var f = new F(); //Instantiate the object console.log(f.hasOwnProperty("name")); //Returns true, indicating that the name currently called is an owned property console.log(f.name); //Returns the string "Own Property"

All prototype properties of the constructor function (properties contained in the prototype object) are inherited properties, and when tested using the hasOwnProperty() method, false is returned. However, for the prototype object itself, these prototype properties are the prototype object’s 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")); //Returns false, indicating that toString() is Date's own property var d = Date.prototype;
console.log(d.hasOwnProperty("toString")); //Returns true, indicating that toString() is a Date.prototype property

The hasOwnProperty() method can only determine whether the specified object contains a property with the specified name. It cannot check whether a property is included in the object prototype chain, so the properties that can be detected must be object members.

Example 3

The following example demonstrates the range of properties that can be detected by the hasOwnProperty() method.

var o = { //Object literal o1 : { //Child object literal o2 : { //Grandchild object literal name : 1 //Grandchild object literal property}
  }
};
console.log(o.hasOwnProperty("o1")); //Returns true, indicating that o1 is o's own property console.log(o.hasOwnProperty("o2")); //Returns false, indicating that o2 is not o's own property console.log(o.o1.hasOwnProperty("o2")); //Returns true, indicating that o2 is o1's own property console.log(o.o1.hasOwnProperty("name")); //Returns false, indicating that name is not o1's own property console.log(o.o1.hasOwnProperty("name")); //Returns true, indicating that name is not o2's own property

This concludes this article about the JS hasOwnProperty() method for detecting whether a property is an object's own property. For more related JS hasOwnProperty content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the idea of ​​implementing dynamic columns in angularjs loop object properties
  • JavaScript removes unnecessary properties of an object
  • When the springboot post interface accepts json, when it is converted to an object, the properties are all null.
  • Several ways to easily traverse object properties in JS
  • How to delete a property of an object in JavaScript
  • Use of hasOwnProperty method of js attribute object
  • Parsing javascript Date object properties and methods through examples
  • Detailed explanation of dynamic addition, deletion, modification and query of properties when converting Java objects to JSON
  • When converting an object to json, java jackson ignores a property operation of the sub-object
  • Three properties of javascript objects

<<:  A simple method to implement scheduled backup of MySQL database in Linux

>>:  MySQL updates a field in a table to be equal to the value of a field in another table

Recommend

Three Ways to Find the Longest Word in a String in JavaScript (Recommended)

This article is based on the Free Code Camp Basic...

Detailed explanation of putting common nginx commands into shell scripts

1. Create a folder to store nginx shell scripts /...

Do you know the meaning of special symbols in URL?

1.# # represents a location in a web page. The ch...

Solution to the problem of data loss when using Replace operation in MySQL

Preface The company's developers used the rep...

Front-end JavaScript thoroughly understands function currying

Table of contents 1. What is currying 2. Uses of ...

How to successfully retrieve VMware Esxi root password after forgetting it

Prepare a CentOS6 installation disk (any version)...

The principle and implementation of two-way binding in Vue2.x

Table of contents 1. Implementation process 2. Di...

Detailed explanation of the pitfalls of nginx proxy socket.io service

Table of contents Nginx proxies two socket.io ser...

Detailed tutorial on building nextcloud private cloud storage network disk

Nextcloud is an open source and free private clou...

Example of how to use CSS3 to layout elements around a center point

This article introduces an example of how CSS3 ca...

Enterprise-level installation tutorial using LAMP source code

Table of contents LAMP architecture 1.Lamp Introd...

MySQL 8.0.21 installation tutorial with pictures and text

1. Download the download link Click download. You...

js implements array flattening

Table of contents How to flatten an array 1. Usin...

7 interesting ways to achieve hidden elements in CSS

Preface The similarities and differences between ...