Home > Article > Web Front-end > Code to determine whether an object has an attribute with a specified name in JavaScript_Basic knowledge
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.