Several methods for determining object types in Javascript

Source: Internet
Author: User
We know that the operators for detecting object types in Javascript include typeof, instanceof, And the constructor attribute of the object. 1) typeof operator typeof is a unary operator, the returned result is a string indicating the number of operations. For example, "Number", "string", "Boolean", "object", "function", and "undefined" (used to determine whether a variable exists ). However, typeof has limited capabilities. For date and Regexp types, "object" is returned ". For example, typeof {}; // "object" typeof []; // "object" typeof new date (); // "object" is useful only when the object and the original type are differentiated. To partition one object type and another object type, you must use other methods. For example, instanceof operator or constructor of an object. 2) instanceof operator. The instanceof operator requires that the Operation number on the left is an object, and the operation number on the right is the name or constructor of the object class. If the object is a class or constructor instance, The instanceof operator returns true. If the object is not an instance of the specified class or function, or the object is null, false is returned. For example, [] instanceof array; // true [] instanceof object; // true [] instanceof Regexp; // falsenew date instanceof date; // true, you can use the instanceof operator to determine whether the object is of the array type: function isarray (ARR) {return arr instanceof array;} 3) constructor attribute. In JavaScript, each object has a constructor attribute, which references the constructor that initializes the object and is often used to determine the type of an unknown object. If a knowledge value is given, the typeof operator is used to determine whether it is the original value or an object. If it is an object, you can use the constructor attribute to determine its type. Therefore, the function used to determine the array can also be written as follows: function isarray (ARR) {return typeof arr = "object" & arr. constructor = array;} in many cases, we can use the instanceof operator or the constructor attribute of an object to check whether the object is an array. For example, many JavaScript frameworks use these two methods to determine whether an object is of the array type. However, it fails to detect arrays on the Cross-frame page. The reason is that the arrays created in different frameworks (IFRAME) do not share their prototype attributes with each other. For example, copy the Code as follows: <SCRIPT> window. onload = function () {var iframe_arr = new window. frames [0]. array; alert (iframe_arr instanceof array); // falsealert (iframe_arr.constructor = array ); // false} </SCRIPT> <body> <IFRAME> </iframe> </body> A precise detection method is displayed on ajaxian, call the tostring () method across prototype links: object. prototype. tostring (). The preceding cross-framework issues can be solved. After object. Prototype. tostring (o) is executed, the following steps are performed: 1) obtain the class attribute of object o. 2) connection string: "[object" + Result (1) + "]" 3) return result (2) for example: object. prototype. tostring. call ([]); // return "[object array]" object. prototype. tostring. call (/REG/ig); // return "[object Regexp]", so that we can write a robust function to determine whether an object is an array: copy the Code as follows: function isarray (ARR) {return object. prototype. tostring. call (ARR) = "[object array]";} This method has been recognized by many JavaScript masters abroad. This method will be used to detect arrays in the forthcoming jquery 1.3 release. Prototype. A Maintainer of JS writes the following function to obtain the object type name function _ getclass (object) {return object. prototype. tostring. call (object ). match (/^ \ [object \ s (. *) \] $/) [1] ;}; Extension: used to detect various object types: copy the Code as follows: VaR is = {types: ["array ", "Boolean", "date", "Number", "object", "Regexp", "string", "window", "htmldocument"]} For (VAR I = 0, c; C = is. types [I ++];) {is [c] = (function (type) {return function (OBJ) {return object. prototype. tostring. call (OBJ) = "[object" + Type + "]" ;}}) (c);} alert (is. array ([]); // truealert (is. date (new date); // truealert (is. regexp (/REG/ig); // 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.