The topic of JavaScript judgment

Source: Internet
Author: User

JavaScript has arrays, objects, functions, strings, Booleans, and Symbol,set,map,weakset,weakmap.

Judging these things is also a lot of pits, like the original typeof,instanceof have some bugs, some can meet 90% of the situation, but also some less satisfactory.

So each big class library has a number of functions to judge, with is the beginning of XXX.

1, Judge null
typeof null//' object ', so it cannot be judged by typeof. A pit of typeof.

Null everyone is judged by Null===null. jquery A underscore is written like this:

function isNull (obj) {return obj===null}

2.undefined

typeof undefined=== "undefined", can not directly write undefined, and JQ and other class libraries and underscore are so written:

function isundefined (obj) {return obj===void 0;}

3.NaN

typeof nan=== "Number", JS built-in IsNaN in the input string, the object is returned true, the name of the white blind, but it is some people use this to judge the figure,!isnan. So the time to Judge Nan is:

function IsNaN (obj) {return obj!==obj}

Jquery,underscore and other such libraries are so judged, it seems that we have similar views.

3.Window Object

jquery's Judgment Window object

// Judging window function IsWindow () {    window==document//IE678 true    Document==window//  IE678 false}

3. Determine the base type number,boolean,function,string.

// typeof judgment String,number,boolean,function can meet 90% of demand // but .....  typeofnew Boolean (1); // "Object" typeof New Number (1); // "Object" typeof New String (1); // "Object"

These to judge also quite simple, on the underscore source bar.

_.isfunction/_.isstring/_.isnumber/_. IsDate /_. isregexp all the same. Use Object.prototype.toString.call (obj) to determine

Just tried, the Object.toString.call and the toString on the prototype chain return a different -0-.

function (obj) {    return Object.protorype.toString.call (obj) = = ' [Object Function] ';};

Underscore's Boolean judgment is still quite complete, including the literal, the Boolean of the object form:

function (obj) {    returntruefalse | | tostring.call (obj) = = ' [Object Boolean] ';};

4.Object

TypeOf too many things are object, see underscore judge

function (obj) {    return obj = = = Object (obj);};
// here typeof NULL // ' object ' typeof []//objecttypeof//"Object"typeof//  IE678 "Object"

5.element

function (obj) {    return !! (obj && Obj.nodetype = = 1);};

6.Arguments

This is underscore's judgment arguments, and the source code, the note is very clear also does not say much.

_.isarguments =function(obj) {returnTostring.call (obj) = = ' [Object Arguments] ';};//Verify the Isarguments function and redefine the Isarguments method if the running environment does not properly validate the arguments type of dataif(!_.isarguments (arguments)) {    //for environments where the arguments type cannot be validated by ToString, validation is done by invoking the unique callee method of arguments_.isarguments =function(obj) {//Callee is a property of arguments that points to a reference to the function itself that arguments belongs to        return!! (obj && _.has (obj, ' callee ')); };}

7.Array

Judging the array is the most troublesome, the front is OK, the array default 0,1,2,3 subscript, a length property, some array method, judge the time also to ensure that obj is not an array of classes, but also to ensure that it is not arguments.

Let's take a look at these class libraries. Explore the distances of array

Start is

return arr.instanceof Array;

Later

return !! arr&&arr.constructor==array;
return typeof arr.sort== ' function '
return Try {    Array.prototype.toString.call (o);     return true ;} Catch (e) {    returnfalse;}

Judging from instanceof, constructor judgment to array of the ToString method of judgment, it is exhausted mind, the latest version of the jquery judgment array seems to put this many methods together, write the old long.

Underscore's judgment is judged by its tostring.call:

_.isarray = Nativeisarray | | function (obj) {    return tostring.call (obj) = = ' [Object Array] ';};

If JS native support IsArray first with the original, this nativeisarray is set to the beginning of the array IsArray method.

The big frame also has the class array judgment, this more troublesome, the old long code, the judgment principle is the class array the length property to be rewritten, but the array's length attribute cannot be re, the code does not post.

Summarize some of the pits of native judgments.

//TypeOf's Traptypeof NULL//' object 'typeof[]//ObjecttypeofDocument.childnodes//Safari "function"typeofDocument.createelement (' embed ')//ff3-10 "function"typeofDocument.createelement (' object ')//ff3-10 "function"typeofDocument.createelement (' applet ')//ff3-10 "function"typeof/\d/ItypeofWindow.alert//IE678 "Object"//instanceof cannot cross-document an array instance inside an IFRAME is not a parent window array instanceinstanceof//Contructor's Trap//the constructor property of the Dom BOM object in the old version of IE is not exposed.

Well, write so much today, thank you for watching, if there are errors, please point out, we learn together ~

  

The topic of JavaScript judgment

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.