Tool methods in jquery $. isFunction, $. isArray (), $. isWindow ()

Source: Internet
Author: User
Tags number strings

Tool methods in jquery $. isFunction, $. isArray (), $. isWindow ()

 

In javascript's judgment on the variable type, we explained in jquery$.type()Implementation principle. Of course, in addition to providing$.typeIn addition to the tool methods, 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$.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()Also passed$.type()Implemented:

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

Whether it is array literal or useNewAll variables created by keywords can be used.$.isArray()Determine whether it is of the array type. In jquery source code, $. isArray calls the isArray method provided by native Array. Because in a browser of the higher version,isArrayTo determine whether the variable is of the array type.

isArray: Array.isArray
3. $. isWindow ()

$.isWindow()Is used to determine whether the current variable isWindowSuch:

$.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, Similarly:

window.window.window.window === window;

You can keep repeating.

In the code, why should we first determine whether obj is null? The Code throws an exception when determining whether null or undefined has the window attribute: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 I use$.type()==numberTo determine. 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

Use$.isNumeric()Ability to determine"-10","0xFF"In this way, numbers of the string type, and$.type()It 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]This type of array can be directly used for subtraction, and can also be converted to a number 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.

Which of the other types is obtained after parseFloat () conversion?NaN, NaN cannot be compared with 0 regardless of the Operation passed, 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 using such code$.isNumeric([123])Returns true after running. In fact, it is an array type. But fortunately, it has been fixed in later versions.

5. $. isEmptyObject ()

$.isEmptyObject()It is not used to determine the type of a variable, but to determine whether the object type is null and does not contain any attribute.

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 jqueryfor~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.

 

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.