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

Introduction to several ways to introduce CSS in HTML

Table of contents 1. Embed CSS styles directly in...

SSM VUE Axios Detailed Explanation

Table of contents How to display SQL log? ? Descr...

Pull-down refresh and pull-up loading components based on Vue encapsulation

Based on Vue and native javascript encapsulation,...

Design Tips: We think you will like it

<br />Looking at this title, you may find it...

Full-screen drag upload component based on Vue3

This article mainly introduces the full-screen dr...

A brief introduction to web2.0 products and functions

<br />What is web2.0? Web2.0 includes those ...

How to use CocosCreator object pool

Table of contents Preface: Specific operations St...

jQuery implements percentage scoring progress bar

This article shares the specific code of jquery t...

Summary of MySQL slow log practice

Slow log query function The main function of slow...

React uses emotion to write CSS code

Table of contents Introduction: Installation of e...

Detailed steps to delete environment variables in Linux

How to delete environment variables in Linux? Use...

Docker Compose network settings explained

Basic Concepts By default, Compose creates a netw...

WeChat applet picker multi-column selector (mode = multiSelector)

Table of contents 1. Effect diagram (multiple col...

How CSS affects the white screen time during initial loading

Rendering pipeline with external css files In the...