Detailed explanation of the properties and instance usage of hasOwnProperty in js

Detailed explanation of the properties and instance usage of hasOwnProperty in js

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.

Examples

var 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 exists

var 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 attributes

function 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 object

When 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 name

JavaScript 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:
  • The JS hasOwnProperty() method detects whether a property is an object's own property.
  • Detailed explanation of the difference between in and hasOwnProperty in JavaScript
  • JavaScript hasOwnProperty() function example
  • A brief discussion on the difference between using in and hasOwnProperty to obtain object properties in js
  • Detailed discussion of the difference between using in and hasOwnProperty to obtain object properties in js
  • hasOwnProperty(), propertyIsEnumerable(), and isPrototypeOf() in JS

<<:  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)

Recommend

Detailed steps to install Docker 1.8 on CentOS 7

Docker supports running on the following CentOS v...

In-depth understanding of JavaScript callback functions

Table of contents Preface Quick Review: JavaScrip...

The difference between HTML name id and class_PowerNode Java Academy

name Specify a name for the tag. Format <input...

Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial

Preparation 1. Environmental Description: Operati...

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

JS implements sliding up and down on the mobile terminal one screen at a time

This article shares with you the specific code of...

How to use node scaffolding to build a server to implement token verification

content Use scaffolding to quickly build a node p...

Detailed explanation of Linux text processing tools

1. Count the number of users whose default shell ...

Detailed example of reading speed of js objects

1. Accessing literals and local variables is the ...

How to deploy Solidity smart contracts using ethers.js

If you have developed DApps on Ethereum, you may ...

A brief understanding of the differences between MySQL InnoDB and MyISAM

Preface MySQL supports many types of tables (i.e....

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

JavaScript to achieve simple image switching

This article shares the specific code for JavaScr...

MySQL 8.0.15 installation tutorial for Windows 64-bit

First go to the official website to download and ...