A detailed explanation of the types of JavaScript and jquery _jquery

Source: Internet
Author: User
Tags object object

For the type of judgment, JavaScript uses typeof.

Chestnuts:

Console.log (typeof null);      Object
Console.log (typeof []);       Object
Console.log (typeof {});       Object
Console.log (typeof new Date ());   Object
Console.log (typeof new Object);   Object
Console.log (typeof function () {});  function
Console.log (typeof alert);      function
Console.log (typeof 1);        Number
Console.log (typeof "ABC");      String
Console.log (typeof true);      Boolean

As you can see, typeof does not accurately determine each type of data, such as null and array, and so on. Therefore, JavaScript does not recommend using typeof for data types.

So how to make specific judgments?? Take a look at grammar < ( ̄3 ̄) >!

{}.tostring.call (obj);

Chestnuts:

Console.log ({}.tostring.call (null));      [Object Null]
console.log ({}.tostring.call ([]));       [Object Array]
console.log ({}.tostring.call ({}));       [Object Object]
console.log ({}.tostring.call (New Date ());   [Object Date]
console.log ({}.tostring.call (function () {}));  [Object Function]
console.log ({}.tostring.call (new object));   [Object Object]
console.log {}.tostring.call (alert);      [Object Function]
console.log ({}.tostring.call (1));        [Object number]
Console.log ({}.tostring.call (' abc '));      [Object String]
console.log ({}.tostring.call (true));      [Object Boolean]

Haha, is not a glance Ah!!

So if you're using jquery, you don't have to be so troublesome, you can use the tool method $.type () to judge

Chestnuts:

Console.log ($.type (null));         Null
console.log ($.type ([]));          Array
Console.log ($.type ({}));          Object
Console.log ($.type (1));           Number
... Not all finished, results and {}.tostring.call (obj); it's the same.

actually {}.tostring.call (obj) is the most important piece of code (⊙o⊙) for the implementation of the $.type () method in jquery. Oh, amazing! Quickly go to the jquery source to look for it ~ ~

The above is a small series for you to bring about JavaScript and jquery type of the full content of the judgment, I hope that we support cloud Habitat Community ~

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.