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

How to connect to MySQL visualization tool Navicat

After installing Navicat The following error may ...

Native JS to achieve cool paging effect

This article uses an example to share with you a ...

Vue dynamic menu, dynamic route loading and refresh pitfalls

Table of contents need: Ideas: lesson: Share the ...

CSS sets the list style and creates the navigation menu implementation code

1. Set the list symbol list-style-type: attribute...

MySQL 5.7.21 installation and configuration tutorial

The simple installation configuration of mysql5.7...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

Summary of JavaScript custom object methods

Table of contents 1. Use object to create an obje...

This article will show you what Vite does to the browser's request

Table of contents Working principle: What does th...

Analysis on the problem of data loss caused by forced refresh of vuex

vuex-persistedstate Core principle: store all vue...

Vue custom v-has instruction, steps for button permission judgment

Table of contents Application Scenario Simply put...

Sharing of web color contrast and harmony techniques

Color contrast and harmony In contrasting conditi...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

MySQL 8.0.14 installation and configuration method graphic tutorial (general)

MySQL service 8.0.14 installation (general), for ...