jquery source code parsing-jquery Tools and methods (2)

Source: Internet
Author: User
Tags hasownproperty

The following methods are explained in this article:

Isfunction (): Whether it is a function

IsArray (): is the array

IsWindow (): Is window

IsNumeric () is a number

Type (): Determine data type

Isplainobject (): Is an object argument

Isemptyobject (): Is an empty object

Isfunction Method:

This method is very simple, to determine whether the object is a function, return bool type, look at the source code:

// See Test/unit/core.js for details concerning isfunction.    // Since version 1.3, DOM methods and functions like alert    // aren ' t supported. They return false on IE (#2968).    function (obj) {        return jquery.type (obj) = = = "function";    },

The source code is very simple, by calling the Jquery.type method to determine whether the return value is a function, it is important to note that the comment section, note that in the old IE version of the browser, from 1.3 onwards to the DOM method and the native method is not supported, then return is false.

IsArray Method:

IsArray:Array.isArray,

You can see the method of using native arrays directly, which is also because the native method is faster, so there is a native method or a native method at the end.

IsWindow Method:

function (obj) {        returnnull && obj = = = Obj.window;    },

This method is used to determine whether a Window object is used, here are two conditions, first of all:

Obj! = NULL, and null for comparison, only null and undefined comparisons will be true, so here are the first two cases to be judged to avoid the subsequent condition error.

obj = = Obj.window If it is a Window object, the following property will have a window property and can be called indefinitely, for example: Window.window.window.window.

Let's not say a specific reason here, wait until later to explain.

IsNumeric Method:

function (obj) {        return !isnan (parsefloat (obj)) && isfinite (obj);    },

This method is used to determine whether the number type, then why this is so troublesome, rather than directly with the TypeOf method to judge, because:

Console.log (typeof// number

As you can see, the return is also number when you use the TypeOf method to determine Nan, so you cannot use the native method for reliability.

In the first condition, the parameter is converted, if possible, that is the number type, if not converted, the return is Nan, also does not satisfy the condition.

The second condition uses a Isfinite method, which is used to determine whether the number is within the available range, because the computer calculates a limited amount of data, sometimes it may pass in a very large number, beyond the computing range of the computer, and this also returns false.

// false

Type method:

The type method returns the data type of the passed-in parameter, such as:

  function A () {};   // function

Source:

function (obj) {        ifnull  ) {            return  String (obj);        }         // Support:safari <= 5.1 (functionish RegExp)        return typeof typeof obj = = = "function"?             || "Object" :            typeof  obj;    },

The passed arguments are first judged, and if null or undefined, both types of strings are returned directly.

Next, determine if the parameter is an object or function, and if so, run class2type[core_tostring.call (obj)] | | "Object", otherwise directly return typeof obj, because this situation can only be the basic type, with typeof judgment is enough.

Here is a detailed description of the most important sentence:

class2type[Core_tostring.call (obj)] | | "Object"

The Core_tostring object is actually the {}.tostring method. In Source 56 line:

core_tostring = class2type.tostring,

Can be found, the following describes the ToString method:

  var arr=New  Array ();   // [Object Array]

This method can also be used to determine the date, function, and so on, we can know that if the original method, such as the date type, it returns an object, such as:

var arr=New  Array (); Console.log (typeof//Object

So it's best to use this situation for type judgment, which is fairly accurate.

Here's a look at what Class2type has saved in this object:

// Populate the Class2type map function (i, name) {    "[object" + name + "]"] = name.tolowercase ();});

As you can see, this method puts all types into this object and then spells out the corresponding property name and shorthand value for [object +name], which can then be used to find the corresponding shorthand data type directly from the property name.

Isplainobject Method:

This method is to determine whether the object is an argument. return bool value, Source:

Isplainobject:function(obj) {//Not plain objects:        //-Any object or value whose internal [[Class]] property is not "[Object Object]"        //-DOM nodes        //-Window        if(Jquery.type (obj)!== "Object" | | obj.nodetype | |Jquery.iswindow (obj)) {            return false; }        //Support:firefox <20        //The Try/catch suppresses exceptions thrown when attempting to access        //the "constructor" property of certain host objects, ie. |window.location|        //https://bugzilla.mozilla.org/show_bug.cgi?id=814622        Try {            if(Obj.constructor &&!core_hasown.call (Obj.constructor.prototype, "isprototypeof" ) ) {                return false; }        } Catch(e) {return false; }        //If the function hasn ' t returned already, we ' re confident that        //|obj| is a plain object, created by {} or constructed with new object        return true; },

First condition:

if (Jquery.type (obj)!== "Object" | | obj.nodetype | | Jquery.iswindow (obj)) {            returnfalse;        }

This is the first judgment, if it is not the obj type, or the node type, or the Window object, then return false directly.

//Support:firefox <20        //The Try/catch suppresses exceptions thrown when attempting to access        //the "constructor" property of certain host objects, ie. |window.location|        //https://bugzilla.mozilla.org/show_bug.cgi?id=814622        Try {            if(Obj.constructor &&!core_hasown.call (Obj.constructor.prototype, "isprototypeof" ) ) {                return false; }        } Catch(e) {return false; }

Here is another case to judge, there is a special situation is to pass the window.location, because the above judgment statement can not judge this situation.

First, determine if the object has constructor, if so, whether there is a isprototypeof method on its prototype, if not, then return False,core_hasown:

Core_hasown = Class2type.hasownproperty,

From here you can see the hasOwnProperty method that called object before.

Why does this code need to use try, the note is because in the version of Firefox 201, if you call Window.location.contructor multiple times, there may be too much recursion this error, For specific reasons please see: https://bugzilla.mozilla.org/show_bug.cgi?id=814622

Isemptyobject Method:

This method is used to determine whether the object is empty.

function (obj) {        var  name;          for inch obj) {            returnfalse;        }         return true ;    },

This looks like it is handled directly with the for-in method, and if there are attributes inside the object, it is returned with a for-in, false. In addition, the for-in method iterates through only the properties that it adds to itself, without traversing the own property.

jquery source code parsing-jquery Tools and methods (2)

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.