How to determine whether an object has a certain attribute in es6
Judgment method: 1. Use the "object.property name! == undefined" statement to determine. If the return value is true, then there is a certain attribute on the object; 2. Use the "'property name' in object" statement , if it returns true, there is a certain attribute; 3. Use the "object.hasOwnProperty('property name')" statement.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 Determine whether the object has a certain attribute
Method 1: Point (.) undefined judgment
We know that the attribute value of an object can be obtained through dots or square brackets. If the attribute does not exist on the object, undefined will be returned. This method can determine the own properties and inherited properties of the specified object. If the object itself does not have a detected property and the property is on the prototype chain, the property value on the prototype chain will be returned.
// 创建对象 let obj = { name: 'Scarlett', age: 37 } console.log(obj.name !== undefined) // true 自身属性存在 console.log(obj.gender !== undefined) // false gender属性不存在 // 在原型上添加一个可枚举属性 Object.prototype.nationality = 'America' // 在obj对象上添加一个不可枚举属性 Object.defineProperty(obj, 'occupation', { value: 'actress', enumerable: false })
Simply, we can use the return value of Object.propertyName !== undefined to determine whether the object contains a certain property. But there is one situation, that is, if the attribute name exists and the attribute value is undefined, the desired result cannot be returned.
// 新增一个值为undefined的属性 obj.birthday = undefined console.log(obj.birthday !== undefined) // false
Well, we can use the in operator to solve this problem.
Method 2: in operator
This method can determine whether a certain attribute exists in the own attributes and inherited attributes of the specified object, and returns true if it exists. The in operator can also detect properties on the prototype chain. The syntax of the
'name' in obj // true 自身属性存在 'occupation' in obj // true 不可枚举属性存在 'nationality' in obj // true 继承属性 'birthday' in obj // true 值为undefined的属性
in operator is also very simple. The scope and effect are the same as dots (.) or square brackets ([]). The difference is that attributes with a value of undefined can also be judged normally.
The limitation of the above two methods is that they cannot accurately distinguish between self-owned properties and properties on the prototype chain. If you want to check whether your own properties exist, you need Object.hasOwnProperty().
Method 3: Object.hasOwnProperty()
Object.hasOwnProperty() is used to determine whether the specified object itself contains a certain attribute (not Inherited), returns a Boolean value.
obj.hasOwnProperty('name') // true 自身属性 obj.hasOwnProperty('occupation') // true 不可枚举属性 obj.hasOwnProperty('birthday') // true obj.hasOwnProperty('nationality') // false 原型链上继承的属性
This method will filter out those inherited attributes and return true when the detected attribute is its own attribute.
【Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of How to determine whether an object has a certain attribute in es6. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



async is es7. async and await are new additions to ES7 and are solutions for asynchronous operations; async/await can be said to be syntactic sugar for co modules and generator functions, solving js asynchronous code with clearer semantics. As the name suggests, async means "asynchronous". Async is used to declare that a function is asynchronous; there is a strict rule between async and await. Both cannot be separated from each other, and await can only be written in async functions.

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.
