Home > Web Front-end > JS Tutorial > Code analysis jQuery four static methods use_jquery

Code analysis jQuery four static methods use_jquery

WBOY
Release: 2016-05-16 15:49:17
Original
1373 people have browsed it

Each method in jQery has a corresponding code analysis, which is very detailed. Please see the introduction below:

isFunction method

Object used to test whether it is a function

Example:

function stub() {
  }
var objs = [
      function () {},
      { x:15, y:20 },
      null,
      stub,
      "function"
     ];
    jQuery.each(objs, function (i) {
    var isFunc = jQuery.isFunction(objs[i]);
    $("span:eq( " + i + ")").text(isFunc);
 })

Copy after login

Run result:

Copy code The code is as follows:

[ true,false,false,true,false ]

Source code analysis:

// 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).
isFunction: function( obj ) {
   return jQuery.type(obj) === "function";
},

Copy after login

First of all, let me tell you that there is a bug since version 1.3. Some DOM methods and functions such as alert will return false in IE. After looking at this bug, because the toString method and the valueOf method will be rewritten, someone proposed to use The instanceof method detects but there is still a problem in ie6. So far, this bug has not been closed. For details, you can refer to the bug page of the official website. Because I am analyzing 1.7.1, I will follow this version first. This method is to simply call the type method to determine whether the return result is a string function

isArray method

Object used to test whether it is an array

Example:

$("b").append( " + $.isArray([]) );//<b>true</b>

isArray: Array.isArray || function( obj ) {
    return jQuery.type(obj) === "array";
 },

Copy after login

Use the return result of the type method directly like isFunctoin

isNumeric method

Determines whether its argument is a number.

The $.isNumeric() method checks whether its argument represents a numeric value. If so, it returns true. Otherwise, it returns false. The parameter can be of any type

Example:

$.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
Copy after login

Source code analysis:

Copy code The code is as follows:

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

This method does not determine the Number type but a type that looks like a number. As long as the parameter passed in contains a number, it will return true. First, use the parseFloat method to convert the parameter into an array. This method will retain the numerical part of the parameter for filtering. Remove other parts, if the result is not NaN and does not exceed the maximum value, it is true, otherwise it returns false

isWindow method

Used to test whether it is a window object

<div class="codetitle"><span><a style="CURSOR: pointer" data="41657" class="copybut" id="copybut41657" onclick="doCopy('code41657')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code41657">isNumeric: function( obj ) {
    return !isNaN( parseFloat(obj) ) && isFinite( obj );
},
</div>
Copy after login

A rough method to determine whether the object is a window. If the object passed in is satisfied and has a setInterval method, the object is considered to be a window object. Now this method has been changed to determine whether it is a window object. The details will be analyzed later. I hope you all enjoy the above content.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template