Type determination in jQuery and jQuery type judgment

Source: Internet
Author: User

Type determination in jQuery and jQuery type judgment

There is a type method in JQuery, which is written in 1.11.2.

 1 var class2type = {}; 2 var toString = class2type.toString; 3 jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { 4     class2type[ "[object " + name + "]" ] = name.toLowerCase(); 5 }); 6 type: function( obj ) { 7     if ( obj == null ) { 8         return obj + ""; 9     }10     return typeof obj === "object" || typeof obj === "function" ?11         class2type[ toString.call(obj) ] || "object" :12         typeof obj;13 }

The core is to use Array. prototype. toString. call. Because the native toString () method of the Object is called on any value, a string in the format of [object NativeConstructorName] is returned. Each Class has an internal [[Class] attribute, which specifies the name of the constructor in the preceding string.

This is because the built-in type detection mechanism of js is not completely reliable.

For example, typeof null outputs "object"

Typeof alert outputs "object" in IE8"

Typeof [] outputs "object"

If instanceof is compared across documents, there will be a lot of problems. If the constructor attribute is used, problems may occur in earlier IE versions (window. constructor is undefined in IE7 ). In addition, the two are not convenient to determine the basic type.

Therefore, to determine whether it is a function, the array can be written in this way.

1 isFunction: function( obj ) {2     return jQuery.type(obj) === "function";3 },4 5 isArray: Array.isArray || function( obj ) {6     return jQuery.type(obj) === "array";7 },

The strange thing is to judge whether it is a window.

1 isWindow: function( obj ) {2     return obj != null && obj == obj.window;3 },

It seems that you can also cheat in disguise, probably because the window object belongs to BOM, and the previous method does not work for it.

Then it is written in isPlainObject, 2.0.1.

 1 isPlainObject: function( obj ) { 2         // Not plain objects: 3         // - Any object or value whose internal [[Class]] property is not "[object Object]" 4         // - DOM nodes 5         // - window 6         if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { 7             return false; 8         } 9 10         // Support: Firefox <2011         // The try/catch suppresses exceptions thrown when attempting to access12         // the "constructor" property of certain host objects, ie. |window.location|13         // https://bugzilla.mozilla.org/show_bug.cgi?id=81462214         try {15             if ( obj.constructor &&16                     !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {17                 return false;18             }19         } catch ( e ) {20             return false;21         }22 23         // If the function hasn't returned already, we're confident that24         // |obj| is a plain object, created by {} or constructed with new Object25         return true;26     }

Here, PlainObject should be an Object such as {}, new Object.

The first if clause can exclude non-Object objects, dom nodes, and windows.

The second if is to exclude BOM objects such as window. location.

Where,

Class2type = {},

Core_hasOwn = class2type. hasOwnProperty

Because the prototype of the constructor of these BOM objects is definitely not Object {}, and they do not have the attribute isPrototypeOf.

Then it is to judge the empty object, that is, the object without the enumerated property.

1 isEmptyObject: function( obj ) {2     var name;3     for ( name in obj ) {4         return false;5     }6     return true;7 }

Another function is used to determine whether it is a number,

//1.11.2isNumeric: function( obj ) {    return !jQuery.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0;},

Returns 2 to an array in parseFloat, for example, [2, 4]. However, if the array is used in the right operation, the result must be false. I don't know why to judge whether it is an array first.

Here, if you input a string such as '123' and '123', it can also be passed.

In versions 1.8.0 and 2.0.1, it is! IsNaN (parseFloat (obj) & isFinite (obj ).

 

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.