Tools used in jquery $.isfunction, $.isarray (), $.iswindow () _jquery

Source: Internet
Author: User
Tags arrays numeric number strings

In JavaScript, in the judgment of variable types, we explain the principle of $.type () implementation in jquery. Of course, jquery offers several other tools, in addition to the $.type tool approach: $.isfunction (), $.isarray (), $.iswindow (), $.isnumeric (), and so on. These methods can be used to see the use of the method name, and here are one by one of the internal details of how these methods are implemented in jquery (2.1.2).

1. $.isfunction ()

$.isfunction () is used to determine whether a variable is a function type, and we take a few examples to see:

$.isfunction (123); False
$.isfunction (TRUE);//False
$.isfunction ([1, 2]);//False
$.isfunction (function () {});/True

function func () {

}
var sfunc = function () {

}
$.isfunction (func);//True
$.isfunction ( SFUNC);/True

As you can see from the example above, in $.isfunction (param), returns true if the incoming Param is a function type, and the other type returns false.
Looking at the source of jquery we can see that $.isfunction () is also implemented through $.type ():

Isfunction:function (obj) {return
	jquery.type (obj) = = "function";
}

2. $.isarray ()

$.isarray () is used to determine whether a variable is an array type. Similarly, we have a few examples to look at the use of $.isarray:

$.isarray (123);  False
$.isarray (TRUE);//False
$.isarray ([1, 2]);//True
$.isarray (New Array (3, 4));//True

Whether it is an array literal or a variable created with the new keyword, you can use $.isarray () to determine that it is an array type. In the jquery source, $.isarray invokes the IsArray method provided by the native array. Because in a newer browser, native JavaScript has been given a IsArray method to determine whether a variable is an array type.
IsArray:Array.isArray
3. $.iswindow ()

$.iswindow () is used to determine whether the current variable is window, such as:

$.iswindow (window); True
$.iswindow ([]); 	False
$.iswindow (null); 	False

In the jquery source code:

Iswindow:function (obj) {return
	obj!= null && obj = = Obj.window;
}

He determines whether obj is a window object by determining whether obj has a window property. Because there is a property window in the Window object, it is himself, therefore: Window.window===window, the same:

Window.window.window.window = = window;

It can be recycled all the way down.

And why is it that the code first determines if obj is null? Because the code throws an exception when determining whether null or undefined has a window attribute: uncaught typeerror:cannot read Property ' window ' of NULL. Therefore, in order to prevent code errors, first determine whether the variable is null, if it is null, it is definitely not a Window object, return false directly, otherwise the variable has no window property.

4. $.isnumeric ()

$.isnumeric () is used to determine whether the current variable is a numeric type, but why I do not use $.type () = "number" to judge it. Let's take a look at a few official examples:

$.isnumeric ("-10"); True
$.isnumeric ();   True
$.isnumeric (0xFF);  True
$.isnumeric ("0xFF");//True
$.isnumeric ("8e5");//True (exponential notation string)
$.isnumeric (3.1415); True
$.isnumeric (+10);  True
$.isnumeric (0144);  True (octal integer literal)
$.isnumeric ("");   False
$.isnumeric ({});   False (Empty object)
$.isnumeric (NaN);  False
$.isnumeric (null);  False
$.isnumeric (true);  False
$.isnumeric (Infinity);//False
$.isnumeric (undefined);//False

Using $.isnumeric () can determine the number of string types such as " -10", "0xFF", and $.type () resolves it to a string type.
In the jquery source, this is how the variable type is judged:

Isnumeric:function (obj) {
	//parsefloat NaNs Numeric-cast false positives (null|true|false| "")
	... but misinterprets leading-number strings, particularly hex literals ("0x ...")
	//subtraction forces infinities t o NaN
	//Adding 1 corrects loss of precision from parsefloat (#15100) return
	!jquery.isarray (obj) && (ob J-parsefloat (obj) + 1 >= 0;
}

First, determine if the variable is an array type, or return false directly. But why do we have to determine whether a variable is an array type? Because [123] arrays of such types can be directly subtracted, they can also be converted to numbers by parsefloat (["123"]):

[M]-[345]// 		parsefloat ([123])// 	123
parsefloat ([[]]//345

Therefore, it cannot be directly converted by parsefloat () and then judged. The first thing to decide is whether the variable is an array, or not to make the next decision:

(Obj-parsefloat (obj) + 1) >= 0

Pure numbers, string-type numbers, numbers starting at 0 (8), arrays at the beginning of 0x (16), and so on, can be converted to 10-digit numbers through parsefloat () normal. After the expression of the above operation, it is definitely greater than 0. But why add 1 to it? The code also explains that by parsefloat () conversion to, will result in the loss of precision, so after +1, the results of the operation more accurate.

and other types of parsefloat () after the conversion of the Nan,nan, no matter how the operation, can not be compared with 0, return false.

In previous versions of jquery (such as 2.0.2):

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

We can find that using such code $.isnumeric ([123]) after running, gets true, and actually, it is the array type. But fortunately, the subsequent version has been repaired.

5. $.isemptyobject ()

$.isemptyobject () is not used to determine the type of a variable, but rather to determine whether an object type is empty and does not contain any attributes.
Starting with JQuery 1.4, this method detects both the properties of the object itself and the attributes inherited from the prototype (and therefore does not use hasOwnProperty). The parameter should be an ordinary JavaScript object, and for other types of objects (DOM elements, original Strings/numbers,host objects) may not provide consistent results across browsers.

$.isemptyobject ({name: "Wenzi"})//False
$.isemptyobject ({})//True

function person () {
	this.name = " Wenzi "
}
$.isemptyobject (new Person ());//False

function Student () {

}
Student.prototype.name = "Wenzi";
$.isemptyobject (New Student ()); False

We can see that both the property of the object itself, or the property on the prototype, will return false as long as it exists.

Isemptyobject:function (obj) {
	var name;
	for [name in obj] {return
		false;
	}
	return true;
}

In jquery, it is detected by For~in. Because For~in is also able to loop to the properties on the prototype, if entered into the loop, it means that obj has attributes, play false, otherwise return true.
6. Summary

jquery also provides a variety of tools, so that we write the JS code more convenient. When you have the opportunity to summarize the other tools and methods.

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.