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

Web Design: The Accurate Location and Use of Massive Materials

Three times of memorization allows you to remembe...

Detailed explanation of the minimum width value of inline-block in CSS

Preface Recently, I have been taking some time in...

A brief discussion on the use and analysis of nofollow tags

Controversy over nofollow There was a dispute bet...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

Analyze how a SQL query statement is executed in MySQL

Table of contents 1. Overview of MySQL Logical Ar...

An example of how Tomcat manages Session

Learned ConcurrentHashMap but don’t know how to a...

Interpretation of Vue component registration method

Table of contents Overview 1. Global Registration...

Use of Linux cal command

1. Command Introduction The cal (calendar) comman...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to ...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...