How to use tools in jquery $. isFunction, $. isArray (), $. isWindow () _ jquery

Source: Internet
Author: User
Tags number strings
This article mainly introduces how to use jquery tools $. isFunction, $. isArray (), $. isWindow (). If you need it, you can refer to the variable type judgment in javascript. We have explained $ in jquery. type () implementation principle. Of course, in addition to $. in addition to the type Tool method, several other tool methods are provided: $. isFunction (), $. isArray (), $. isWindow (), $. isNumeric. The usage of these methods can be seen from the method name. The following describes the internal implementation details of these methods in jQuery (2.1.2.

1. $. isFunction ()

$. IsFunction () is used to determine whether the variable is of the function type. Let's take a look at the following examples:

$.isFunction(123); // false$.isFunction(true);// false$.isFunction([1, 2]);// false$.isFunction(function(){});// truefunction func(){}var sfunc = function(){}$.isFunction(func); // true$.isFunction(sfunc);// true

As shown in the preceding example, in $. isFunction (param), if the input param is of the function type, true is returned. If the input param is of another type, false is returned.
View the jquery source code. 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 the variable is of the array type. We also use several examples to see the usage of $. isArray:

$.isArray(123);  // false$.isArray(true); // false$.isArray([1, 2]);// true$.isArray(new Array(3, 4)); // true

$. IsArray () can be used to determine whether it is an array literal or a variable created using the new keyword. In jquery source code, $. isArray calls the isArray method provided by native Array. In a browser of the higher version, the native JavaScript has provided an isArray method to determine whether the variable is of the array type.
IsArray: Array. isArray
3. $. isWindow ()

$. IsWindow () is used to determine whether the current variable is a window, for example:

$.isWindow(window); // true$.isWindow([]); // false$.isWindow(null); // false

In jQuery source code:

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

It judges whether obj has the window attribute to determine whether it is a window object. Because the window object has a property window, which is his own, so: window. window = window, the same:

Window. window === window;

You can keep repeating.

In the code, why should we first determine whether obj is null? When determining whether null or undefined has the window attribute, the code will throw an exception: Uncaught TypeError: Cannot read property 'window' of null. Therefore, to prevent code errors, first determine whether the variable is null. If it is null, it is definitely not a window object and returns false directly; otherwise, it determines whether the variable has a window attribute.

4. $. isNumeric ()

$. IsNumeric () is used to determine whether the current variable is of the numeric type, but why not use $. type () = "number" for determination. Let's take a look at several official examples:

$.isNumeric("-10"); // true$.isNumeric(16);   // 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

$. IsNumeric () can be used to determine numbers of the string type such as "-10" and "0xFF", and $. type () will be parsed to the string type.
In jquery source code, the variable type is determined as follows:

isNumeric: function( obj ) {// parseFloat NaNs numeric-cast false positives (null|true|false|"")// ...but misinterprets leading-number strings, particularly hex literals ("0x...")// subtraction forces infinities to NaN// adding 1 corrects loss of precision from parseFloat (#15100)return !jQuery.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0;}

First, determine whether the variable is of the array type. If so, return false directly. But why should we first determine whether the variable is of the array type? Because [123] Such Arrays can be directly used for subtraction, and can also be converted to numbers through parseFloat (["123:

[100] - 60 // 40[100] - [60] // 40parseFloat([123]) // 123parseFloat(["345"]) // 345

Therefore, parseFloat () cannot be directly converted and then determined. First, you must determine whether the variable is an array. If not, you must determine the next step:

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

Numbers of the string type, numbers starting with 0 (octal), arrays starting with 0x (hexadecimal), and so on can all be passed through parseFloat () convert to a decimal number. After the above expression operation, it must be greater than 0. But why add 1? The Code also explains that converting to through parseFloat () will cause loss of precision. Therefore, after + 1, the calculation result is more accurate.

The NaN is obtained after parseFloat () Conversion of other types. No matter what calculation is adopted, NaN cannot be compared with 0, and false is returned.

In versions earlier than jquery (for example, 2.0.2:

isNumeric: function( obj ) {return !isNaN( parseFloat(obj) ) && isFinite( obj );}

We can find that, after running the code $. isNumeric ([123]), we get true. In fact, it is an array type. But fortunately, it has been fixed in later versions.

5. $. isEmptyObject ()

$. IsEmptyObject () is not used to determine the type of a variable, but to determine whether the object type is null and does not contain any attributes.
Since jQuery 1.4, this method detects both the attributes of the object and the attributes inherited from the prototype (hasOwnProperty is not used ). Parameters should be a common JavaScript Object. For other types of objects (DOM elements, original strings/numbers, host objects), they may not provide consistent results across browsers.

$.isEmptyObject({name:"wenzi"}) // false$.isEmptyObject({}) // truefunction Person(){this.name = "wenzi"}$.isEmptyObject(new Person()); // falsefunction Student(){}Student.prototype.name = "wenzi";$.isEmptyObject(new Student()); // false

We can see that, whether it is an object attribute or a prototype attribute, false is returned if it exists.

isEmptyObject: function( obj ) {var name;for ( name in obj ) {return false;}return true;}

In jquery ~ In. Because ~ In is also a property that can be recycled to prototype. If it is in a loop, it indicates that obj has an attribute and false is used; otherwise, true is returned.
6. Summary

Jquery also provides a variety of tool methods, making it easier for us to compile JavaScript code. I will summarize other tools and methods in the future.

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.