1. js will not protect hasOwnProperty from being illegally occupied. If an object happens to have this property, you need to use an external hasOwnProperty function to get the correct result. 2. When checking whether a property on an object exists, hasOwnProperty is the only method available. Examplesvar foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons' }; foo.hasOwnProperty('bar'); // always returns false // Use hasOwnProperty of other object and set its context to foo ({}).hasOwnProperty.call(foo, 'bar'); // true Knowledge point expansion: Determine whether the attribute existsvar o = new Object(); o.prop = 'exists'; function changeO() { o.newprop = o.prop; delete o.prop; } o.hasOwnProperty('prop'); // true changeO(); o.hasOwnProperty('prop'); // false Determine own attributes and inherited attributesfunction foo() { this.name = 'foo' this.sayHi = function () { console.log('Say Hi') } } foo.prototype.sayGoodBy = function () { console.log('Say Good By') } let myPro = new foo() console.log(myPro.name) // foo console.log(myPro.hasOwnProperty('name')) // true console.log(myPro.hasOwnProperty('toString')) // false console.log(myPro.hasOwnProperty('hasOwnProperty')) // fail console.log(myPro.hasOwnProperty('sayHi')) // true console.log(myPro.hasOwnProperty('sayGoodBy')) // false console.log('sayGoodBy' in myPro) // true Iterate over all the properties of an objectWhen looking at open source projects, you often see source code similar to the following. The for...in loop enumerates all properties of the object and then uses the hasOwnProperty() method to ignore inherited properties. var buz = { fog: 'stack' }; for (var name in buz) { if (buz.hasOwnProperty(name)) { alert("this is fog (" + name + ") for sure. Value: " + buz[name]); } else { alert(name); // toString or something else } } Note hasOwnProperty as the property nameJavaScript does not protect the hasOwnProperty property name, so if there may be an object containing this property name, it is necessary to use an extended hasOwnProperty method to get the correct result: var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons' }; foo.hasOwnProperty('bar'); // always returns false // If you are concerned about this, you can directly use the real hasOwnProperty method on the prototype chain // Use another object's `hasOwnProperty` and call ({}).hasOwnProperty.call(foo, 'bar'); // true // You can also use the hasOwnProperty property on the Object prototype Object.prototype.hasOwnProperty.call(foo, 'bar'); // true This is the end of this article about the properties and example usage of hasOwnProperty in js. For more content about the property usage of hasOwnProperty in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Docker custom bridge docker0 and docker's opening, closing, and restarting command operations
>>: Detailed explanation of box-sizing in CSS3 (content-box and border-box)
1. Open the CentOS 7 virtual machine. 2. Log in t...
Table of contents Preface 1. MySQL enables SSL co...
HTTP Header Explanation 1. Accept: Tells the web s...
Preface scp is the abbreviation of secure copy. s...
Table of contents The difference between hash and...
1. Introduction This article does not have screen...
What is ORM? ORM stands for Object Relational Map...
MySQL service 8.0.14 installation (general), for ...
To perform incremental backup of the MySQL databa...
Table of contents Scene Introduction Plugin Imple...
1. Introduction I have taken over a project of th...
Brief Tutorial This is a CSS3 color progress bar ...
The use of vue3 Teleport instant movement functio...
This morning I planned to use Wampserver to build...
This article shares the specific code of the jQue...