Understanding the role of hasOwnProperty () in JAVASCRIPT

Source: Internet
Author: User
Tags hasownproperty

In JavaScript, The hasOwnProperty function returns a Boolean value indicating whether an object has a property with a specified name.

HasOwnProperty () usage:

Object. hasOwnProperty (proName)
The parameter object is required. An instance of an object.
ProName is required. The string value of an attribute name.

If the object has a property with the specified name, The hasOwnProperty Function Method in JavaScript returns true; otherwise, false. This method cannot check whether the property exists in the prototype chain of the object. The property must be a member of the object. In the following example, all String objects share a common split method. The following code outputs false and true.

Copy codeThe Code is as follows:
Var s = new String ("JScript ");
Print (s. hasOwnProperty ("split "));
Print (String. prototype. hasOwnProperty ("split "));

HasOwnProperty: used to determine whether an object has an attribute or object with the name given by you. However, you must note that this method cannot check whether the property exists in the prototype chain of the object. The property must be a member of the object. The format is as follows:

Js Code

1. object. hasOwnProperty (proName );

Determine whether the name of proName is an attribute or object of the object.

1. Example 1:

Var bStr = "Test String". hasOwnProperty ("split"); // get false because attributes in the prototype chain cannot be detected.

However:

"Test String". split (""); can be called successfully.

2. Example 2:

Var bStr1 = String. prototype. hasOwnProperty ("split"); // the prototype of the String object already has this attribute. Naturally, true is returned.

3. Example 3

Var bObj = ({fnTest: function () {}}). hasOwnProperty ("fnTest"); // returns true because the property in the object exists.

Object

Object itself is not very useful, but you should understand it before learning about other classes. Because the Object in ECMAScript and Java. lang. similar to an object, all objects in ECMAScript are inherited from this Object, and all attributes and methods in the Object will appear in other objects. Therefore, we understand the object Object, to better understand other objects.

An Object has the following attributes:

Constructor
Reference (pointer) of the function used to create an object ). For an Object, this pointer points to the original Object () function.
Prototype
References the object prototype of the object. For all objects, it returns an instance of the Object by default.

The Object also has several methods:

HasOwnProperty (property)
Determines whether an object has a specific attribute. This attribute must be specified with a string. (For example, o. hasOwnProperty ("name "))
IsPrototypeOf (object)
Determine whether the object is a prototype of another object.
PropertyIsEnumerable
Determines whether a given attribute can be enumerated using the for... in statement.
ToString ()
Returns the original string representation of the object. For Object objects, the 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 value returned by this method is the same 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.