Understanding the role of hasOwnProperty () in JavaScript _javascript skills

Source: Internet
Author: User
Tags hasownproperty

The hasOwnProperty function method in JavaScript is to return a Boolean value that indicates whether an object has a property of the specified name.

hasOwnProperty () Use method:

Object.hasownproperty (Proname)
Where the Parameter object is a required option. An instance of an object.
Proname is a required option. A string value for a property name.

If object has a property of the specified name, then the hasOwnProperty function method in JavaScript returns True, and vice versa. This method cannot check whether the object's prototype chain has this property, which must be a member of the object itself. In the following example, all String objects share a common split method. The following code prints false and true.

Copy Code code as follows:

var s = new String ("JScript");
Print (S.hasownproperty ("split"));
Print (String.prototype.hasOwnProperty ("split"));

hasOwnProperty: is used to determine whether an object has a property or object that you give a name to. However, it is important to note that this method cannot check whether the object's prototype chain has the attribute, which must be a member of the object itself. The format is as follows:

JS Code

1. Object.hasownproperty (Proname);

Determines whether the name of the Proname is a property or object of the object.

1. Example one:

var bStr = "Test String". hasOwnProperty ("split"); Get false because the properties in the prototype chain cannot be detected

But:

"Test String". Split (""); it's a successful call.

2. Example two:

var bStr1 = String.prototype.hasOwnProperty ("split"); This property is inherently on the prototype of a string object and returns True Naturally

3. Example Three

var bobj = ({fntest:function () {}}). hasOwnProperty ("Fntest"); Returns true because properties in an object exist

Object objects

Object objects are of little use to themselves, but you should know them before you know other classes. Because object objects in ECMAScript are similar to java.lang.object in Java, all objects in ECMAScript are inherited by this object, and all the properties and methods in the object objects appear in other objects, so it is understood that Ob Ject objects, you can better understand other objects.

Object objects have the following properties:

Constructor
A reference (pointer) to the function that created the object. For object objects, the pointer points to the original object () function.
Prototype
A reference to the object's prototype of the object. For all objects, it returns an instance of Object objects by default.

Object objects also have several methods:

The

    hasOwnProperty (property)
     Determines whether an object has a specific attribute. This property must be specified with a string. (for example, the O.hasownproperty ("name"))
    isprototypeof (object)
    determines whether the object is a prototype of another object.
    propertyisenumerable
    Determines whether a given property can be enumerated with a for...in statement.
    ToString ()
    returns the original string representation of the object. For object objects, ECMA-262 does not define this value, so different ECMAScript implementations have different values.
   valueof ()
    returns the original value that best fits the object. For many objects, the method returns the same value as the return value of ToString ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.