Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the type of javascript variable

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to get the type of javascript variable". In the daily operation, I believe that many people have doubts about how to obtain the type of javascript variable. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to get the type of javascript variable". Next, please follow the editor to study!

The method to get the javascript variable type: 1, use the typeof operator, syntax "typeof variable"; 2, use jQuery's "$.type ()" method; 3, get the type through the constructor.

The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.10.0 version, Dell G3 computer.

In JavaScript, how to accurately obtain the type name of a variable is a frequently used problem.

But often can not get the exact name of the variable, or must use the method in jQuery, here I through typeof, jQuery.type and through the constructor to get the variable type of these three methods in detail.

I hope I can help you.

At first glance, some students may think of the typeof operator.

Use typeof to get basic types

In the JavaScript language, it is given to use the typeof operator to obtain basic type names. (note that it is not a basic type)

This is the whole use of typeof.

01-typeof.htm

Console.log ('typeof of 10 ~' + typeof 10); console.log ('typeof of "a' ~'+ typeof'a'); console.log ('typeof of true ~' + typeof true); console.log ('typeof of {} ~' + typeof {}); console.log ('typeof of / 123 / ~' + typeof / 123 /); console.log ('typeof of function () {} ~' + typeof function () {}); console.log ('typeof of undefined ~' + typeof undefined); console.log ('typeof of null ~ ~' + typeof null)

This is the result.

According to the above print results, summarize the following points that should be paid attention to

Typeof (reference type) except for functions, are all 'object',' such as typeof / 123 /

Typeof null is' object'

Typeof undefined is' undefined', usually, if two equal signs are used, null = = undefined is true.

The common use of conversion to numbers is "10"-0. If the conversion is not successful, NaN is returned. Because of a feature of NaN: NaN! = NaN, it is a common practice to judge whether the conversion is successful or not: (this is also what I found in the source code of jQuery. JQuery source code cannot be read too many times).

("10x"-0) = ("10x"-0); / / the result is false!

Use the method $.type () in jQuery

Now let's see how jQuery does it.

/ / declare an object first in order to map var class2type = {}; / / declare a method of core_toString () to get the original toString () method, because in many objects, toStrintg () has been overridden var core_toString () = class2type.toString / / here do a mapping between the result after toStrintg () and the type name, declare the result after core_toString (), and the value is the type name jQuery.each ("Boolean Number String Function Array Date RegExp Object Error" .split (""), function (I, name) {class2type ["[object" + name + "]] = name.toLowerCase ();})

Because the result of the Object.prototype.toString () method call is as follows

Var core_toString = {} .toString; console.log (core_toString.call (1)); console.log (core_toString.call ("11")); console.log (core_toString.call (/ 123 /)); console.log (core_toString.call ({})); console.log (core_toString.call (function () {})); console.log (core_toString.call ([])); console.log (core_toString.call (true)) Console.log (core_toString.call (new Date ()); console.log (core_toString.call (new Error (); console.log (core_toString.call (null)); console.log (core_toString.call (undefined)); console.log (String (null)); console.log (String (undefined))

The printing result above is the same as that of the above print.

Class2type ["[object" + name + "]"] = name.toLowerCase ()

Agree without prior without previous consultation!

This is the core method of jQuery.type

Type: function (obj) {if (obj = = null) {return String (obj);} / / Support: Safari

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report