Home  >  Article  >  Web Front-end  >  Code to determine whether an object has an attribute with a specified name in JavaScript_Basic knowledge

Code to determine whether an object has an attribute with a specified name in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:36:441072browse

The hasOwnProperty method
returns a Boolean value indicating whether an object has a property with the specified name.
object.hasOwnProperty(proName)

Compatible with WinIE5.5, MacIE-, NN6, Moz, Safari-

Parameters
object

Required. An instance of an object.

proName

Required. A string value for the property name.

Description
If the object has a property with the specified name, the hasOwnProperty method returns true; otherwise, it returns false. This method cannot check whether the property is in the object's prototype chain; the property must be a member of the object itself.

Example
In the following example, all String objects share a common split method. The code below will output false and true.

Copy code The code is as follows:

var s = new String("JScript");
print(s.hasOwnProperty("split"));
print(String.prototype.hasOwnProperty("split"));
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