Javascript: Common Object Attributes and Methods

Source: Internet
Author: User
Tags hasownproperty

All objects in JavaScript are inherited from the object class. Although more specific classification objects such as date and Regexp define their own attributes and methods. However, all created objects also support the attributes and methods defined by the object. These attributes and methods are also particularly interesting due to their versatility.

① Constructor attributes

In JavaScript, each object has a constructor attribute, which references the constructor that initializes this object. For example:

 
VaR d = new date (); D. constructor = date; // true

Since the constructor defines one or more new objects, the constructor attribute helps to determine the type of an object. For example:

 
If (typeof o = "object") & (O. constructor = Date) // then do something with the date object...

② Tostring () method

Tostring () returns a string that represents the value of an object to some extent, and it is called on this object. When JavaScript needs to convert an object into a string, it will call this method. Unfortunately, the default tostring () method does not provide much information. For example, the following lineCodeJust get the string "[object]":

VaR S = {X: 1, Y: 1}. tostring ();

Because this default method does not display too much useful information, many Classes define their own tostring (). For example, if an array is converted to a string, a list of array elements is obtained. Each of these elements is converted to a string. When a function is converted to a string, the result is the function.Source code.

③ Tolocalestring () method

In ecmascript V3 and JavaScript 1.5, the object class defines the tolocalestring () method in addition to its own tostring () method. This method is used to return a localized string representation of an object. The default tolocalestring () method defined by the object does not localize itself. It always returns exactly the same content as tostring. However, subclasses can define their own tolocalestring () versions.

④ Valueof () method

When JavaScript needs to convert an object to a certain basic data type, that is, a number rather than a string, it calls the valueof () method. If an object is used in an environment that requires a basic value, JavaScript will automatically call this method. The default valueof () method does not make any sense. Some built-in objects define their own valueof () methods. After learning the class, constructor, and prototype, we can define a custom object's valueof () method.

⑤ Hasownproperty () method

If an object uses the name specified by a separate string parameter to locally define a non-inherited attribute, the hasownproperty () method returns true; otherwise, false. For example:

 
VaR o = {}; O. hasownproperty ("UNDEF"); // falseo. hasownproperty ("tostring"); // falsemath. hasownproperty ("Cos"); // true

⑥ Propertyisenumerable () method

If the object uses the name specified by a separate string parameter to define a non-inherited attribute, and this attribute can be enumerated in the for/in loop, propertyisenumerable () returns true, otherwise false. For example:

 
VaR o = {X: 1}; O. propertyisenumerable ("X"); // trueo. propertyisenumerable ("Y"); // falseo. propertyisenumerable ("valueof"); // false

Note that all user-defined attributes of an object can be enumerated. Attributes that cannot be enumerated are generally inherited attributes.

7. isprototypeof () method

If the isprototypeof () method belongs to the prototype object of the parameter, true is returned for this method. Otherwise, false is returned.

VaR o ={}; object. prototype. isprototypeof (o); // trueobject. isprototypeof (o); // falseo. isprototypeof (object. prototype); // falsefunction. prototype. isprototypeof (object); // true


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.