Home  >  Article  >  Web Front-end  >  Introduction to the existence method of checking object property in JavaScript_javascript skills

Introduction to the existence method of checking object property in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:23:131069browse

In JavaScript, you can use four methods to check whether an object o owns property x:

1. "x" in o. The in operator can be used to check whether there is a property x in object o. x can be the object's own (Own Property), or it can be inherited from the prototype object; x can be an enumerable property, or a non-enumerable property.

2.o.x. You can confirm whether x exists by accessing the o.x statement and judging whether the result is undefined. Its scope is the same as the in operator. The difference from the in operator is that if the value of a property x is explicitly declared to be undefined in the object o, then the result of the "x" in o operation will be true, and the result of o.x will be undefined.

3.hasOwnProperty(). The o.hasOwnProperty("x") operation is used to determine whether the o object itself has x property. The property o inherited from the prototype object will not be considered. The hasOwnProperty() operation checks both enumerable properties and non-enumerable properties.

4.propertyIsEnumerable(). The o.propertyIsEnumerable("x") operation only checks the enumerable property owned by the o object itself, which is a subset of hasOwnProperty().

Based on the above information, the summary is as follows:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn