Object's hasOwnProperty() method returns a Boolean value, indicating whether the object contains a specific own (non-inherited) property. 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 Reference Links This is the end of this article about the use of the hasOwnProperty method of js property objects. For more relevant js hasOwnProperty content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to create LVM for XFS file system in Ubuntu
>>: Detailed explanation of Mysql master-slave synchronization configuration practice
Table of contents 1. Introduction 2. JDBC impleme...
Description of port availability detection when p...
Effect: First create five vue interfaces 1.home.v...
Scenario Requirements 1. We can use the script fu...
Vue front and back end ports are inconsistent In ...
Table of contents 1. Index Basics 1.1 Introductio...
iOS 1. URL scheme This solution is basically for ...
When I first came into contact with docker, I was...
This article describes the steps to install the p...
CSS3 implements a flippable hover effect. The spe...
This article shares the specific code for WeChat ...
SQL (Structured Query Language) statement, that i...
1. Requirements description For a certain element...
Table of contents Cycle comparison usage Summariz...
Ideas It's actually very simple Write a shell...