Why does jQuery use toString to determine the data type, instead of typeof or instanceof?

Source: Internet
Author: User

[Javascript] // Numbers typeof37 = 'number'; typeof3.14 = 'number'; typeofMath. LN2 = 'number'; typeofInfinity = 'number'; typeofNaN = 'number'; // Despite being "Not-A-number" typeofNumber (1) === 'number'; // but never use this form! // Strings typeof "" = 'string'; typeof "bla" = 'string'; typeof (typeof1) = 'string '; // typeof always return a string typeofString ("abc") === 'string'; // but never use this form! // Booleans typeoftrue = 'boolean'; typeoffalse = 'boolean'; typeofBoolean (true) = 'boolean'; // but never use this form! // Undefined typeofundefined === 'undefined'; typeofblabla === 'undefined'; // an undefined variable // Objects typeof {a: 1 }== 'object '; typeof [1, 2, 4] === 'object'; // use Array. isArray or Object. prototype. toString. call to differentiate regular objects from arrays typeofnew Date () = 'object'; typeofnull = 'object'; typeofnew Boolean (true) = 'object '; // this is confusing. don't use! Typeofnew Number (1) === 'object'; // this is confusing. Don't use! Typeofnew String ("abc") === 'object'; // this is confusing. Don't use! // Functions typeoffunction () {}== 'function'; typeofnew function () === 'function'; typeofMath. sin = 'function'; as shown in the preceding example, typeof cannot determine the array and null, and it cannot determine the type of objects generated using the new operator. As for instanceof, because in JavaScript, all objects are objects, that is, the new Number (2) or new String ('hello') is also an object, so it cannot be determined. However, Object. prototype. toString will always return such a string "[object class]" for any variable, and this class is the name of the constructor of the JavaScript embedded Object. For User-Defined variables, class is equal to object. Therefore, Object. prototype. toString. apply (obj) can be used to accurately obtain the variable data type. The data types obtained through Object. prototype. toString include Date, Object, String, Number, Boolean, Regexp, Function, undefined, null, and Math.

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.