[Javascript] hasOwnProperty

This post is just to remind me that javascript objects have a basic function hasOwnProperty which could determine whether an object has a property with a specified name.

    Parameters : 参数
  • object
    Required. Instance of an object. 对象的一个实例(必须)
  • proName
    Required. String value of a property name. 属性名(必须)
    Return : 返回值
  • true, if object has a property of the specified name
  • false, if it does not
    Example :
var obj = {p1:"v1", p2:"v2"}
if(obj.hasOwnProperty(p1)) {
  console.log("Yes, I have the property p1");
} else {
  console.log("Sorry, I do not have this property ");

If everything is ok, your console will display “Yes, I have the property p1”.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s