Use of hasOwnProperty method of js attribute object

Use of hasOwnProperty method of js attribute object

Object's hasOwnProperty() method returns a Boolean value, indicating whether the object contains a specific own (non-inherited) property.

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

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:
  • Detailed explanation of the idea of ​​implementing dynamic columns in angularjs loop object properties
  • JavaScript removes unnecessary properties of an object
  • When the springboot post interface accepts json, when it is converted to an object, the properties are all null.
  • Several ways to easily traverse object properties in JS
  • How to delete a property of an object in JavaScript
  • The JS hasOwnProperty() method detects whether a property is an object's own property.
  • Parsing javascript Date object properties and methods through examples
  • Detailed explanation of dynamic addition, deletion, modification and query of properties when converting Java objects to JSON
  • When converting an object to json, java jackson ignores a property operation of the sub-object
  • Three properties of javascript objects

<<:  How to create LVM for XFS file system in Ubuntu

>>:  Detailed explanation of Mysql master-slave synchronization configuration practice

Recommend

Using streaming queries in MySQL to avoid data OOM

Table of contents 1. Introduction 2. JDBC impleme...

An example of how Vue implements four-level navigation and verification code

Effect: First create five vue interfaces 1.home.v...

Detailed explanation of zabbix executing scripts or instructions on remote hosts

Scenario Requirements 1. We can use the script fu...

Solve the problem of inconsistent front and back end ports of Vue

Vue front and back end ports are inconsistent In ...

MySQL Index Detailed Explanation

Table of contents 1. Index Basics 1.1 Introductio...

Example of writing mobile H5 to invoke APP (IOS, Android)

iOS 1. URL scheme This solution is basically for ...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

VMware Workstation Pro installs Win10 pure version operating system

This article describes the steps to install the p...

CSS3 achieves flippable hover effect

CSS3 implements a flippable hover effect. The spe...

WeChat applet implements video player sending bullet screen

This article shares the specific code for WeChat ...

MySQL statement arrangement and summary introduction

SQL (Structured Query Language) statement, that i...

In-depth understanding of the life cycle comparison between Vue2 and Vue3

Table of contents Cycle comparison usage Summariz...

Write a mysql data backup script using shell

Ideas It's actually very simple Write a shell...