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:
|
<<: 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
This article is based on the Free Code Camp Basic...
1. Create a folder to store nginx shell scripts /...
1.# # represents a location in a web page. The ch...
Preface Linux does not have a prominent Recycle B...
Preface The company's developers used the rep...
Table of contents 1. What is currying 2. Uses of ...
Prepare a CentOS6 installation disk (any version)...
Table of contents 1. Implementation process 2. Di...
Table of contents Nginx proxies two socket.io ser...
Nextcloud is an open source and free private clou...
This article introduces an example of how CSS3 ca...
Table of contents LAMP architecture 1.Lamp Introd...
1. Download the download link Click download. You...
Table of contents How to flatten an array 1. Usin...
Preface The similarities and differences between ...