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)
Docker supports running on the following CentOS v...
Table of contents Preface Quick Review: JavaScrip...
name Specify a name for the tag. Format <input...
Preparation 1. Environmental Description: Operati...
Preface The three-column layout, as the name sugg...
environment Host IP 192.168.0.9 Docker version 19...
This article shares with you the specific code of...
content Use scaffolding to quickly build a node p...
1. Count the number of users whose default shell ...
1. Accessing literals and local variables is the ...
If you have developed DApps on Ethereum, you may ...
Preface MySQL supports many types of tables (i.e....
Table of contents 1. Use the warehouse to create ...
This article shares the specific code for JavaScr...
First go to the official website to download and ...