JavaScript class Properties

Source: Internet
Author: User

The class property of the object (class attribute) is a string that represents the type information of the object. Both ECMASCRIPT3 and ECMAScript5 do not provide a way to set this property, and there is only one indirect way to query it. The default ToString () Method (inherited from Object.prototype) returns a string in the following format:

[Object class]

Such as:

"[Object Null]", "[Object Undefined]", "[Object String]", "[Object number]", "[Object Array]", "[Object RegExp]", "[Object Boolean] "

Therefore, to get the class of an object, you can call the object's ToString () method, and then extract the characters from the 8th to the penultimate position in the returned character. But the tricky thing is that many of the object-inheriting ToString () methods have rewritten that the Function.call () method must be called indirectly in order to invoke the correct ToString () version. The following classof () function can return the class of any object passed to it:

1 function classof (o) {2     if (o==nullreturn "null"; 3     if return "Undefined"; 4     return Object.prototype.toString.call (o). Slice (applicationcache,-1); 5 }

Such as:

Classof (NULL)//"NULL"

Classof (1)//"number"

Classof ("")//"String"

From the JavaScript authoritative guide 6.8.2

JavaScript class Properties

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.