Js obtains the variable type

Source: Internet
Author: User

Js does not use is_int, is_bool, is_string... functions like PHP to determine the type of the variable, but it may also be used in practical applications. In fact, JS also has this function, but it is not as convenient as in PHP. You only need to use the toString of the Object prototype to complete this function. Some time ago, JQuery also used operations to obtain the variable type. The idea is the same. The following code is used for analysis. In fact, there is no special powerful derivative function, the Code is as follows:

M = Object. prototype. toString // This is to retrieve the toString in the prototype of the Object. The core of the entire function


E = {}; // initialization object


/* This code mainly adds the desired type string to the E loop, adds attributes to the object through [], and converts all type names to lowercase and stores p in E as a JQuery object) */

P. each ("Boolean Number String Function Array Date RegExp Object". split (""), function (a, B ){
E ["[object" + B + "]"] = B. toLowerCase ()
})

/* Execution method for determining the type. The previous String can directly convert null to a variable of the data type, which is not equal to '', false, 0) in js, other variables can be obtained through the toString in the Object prototype, but the returned content is a string such as [object Number]. If the type does not exist, the object is returned. Call usage may check the call and apply operations of js. As long as the obtained type string is in E, the type string specified in E can be returned */

Type: function (){
Return a = null? String (a): E [m. call (a)] | "object"
}


We can see that the entire core code is Object. prototype. toString. call (the variable to be retrieved)

You may wonder why it is not Object. toString. call (the variable to be retrieved)

However, the two methods do not mean the same, and the execution method is not the same. Of course, the second method is incorrect. Prototype: You can view the prototype of protorype in js.


In fact, we can directly use the JQuery type method. Of course, JQuery files must be referenced before they can be used. In this Code is taken from the jQuery v@1.8.0), this code can have some types can not be judged, such as JSON. New versions include.

This article is from the "php In the web" blog, please be sure to keep this source http://php2012web.blog.51cto.com/5585213/1287117

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.