首页 > 语言 > JavaScript > 正文

jQuery源码解读之extend()与工具方法、实例方法详解

2024-05-06 15:19:07
字体:
来源:转载
供稿:网友

本文实例讲述了jQuery源码解读之extend()与工具方法、实例方法。分享给大家供大家参考,具体如下:

使用jQuery的时候会发现,jQuery中有的函数是这样使用的:

$.get();$.post();$.getJSON();

有些函数是这样使用的:

$('div').css();$('ul').find('li');

有些函数是这样使用的:

$('li').each(callback);$.each(lis,callback);

这里涉及到两个概念:工具方法与实例方法。通常我们说的工具方法是指无需实例化就可以调用的函数,如第一段代码;实例方法是必须实例化对象以后才可以调用的函数,如第二段代码。jQuery中很多方法既是实例方法也是工具方法,只是调用方式略有不同,如第三段代码。为了更清晰解释JavaScript中的工具方法与实例方法,进行如下测试。

function A(){}A.prototype.fun_p=function(){console.log("prototpye");};A.fun_c=function(){console.log("constructor");};var a=new A();A.fun_p();//A.fun_p is not a functionA.fun_c();//constructora.fun_p();//prototpyea.fun_c();//a.fun_c is not a function

通过以上测试可以得出结论,在原型中定义的是实例方法,在构造函数中直接添加的是工具方法;实例方法不能由构造函数调用,同理,工具方法也不能由实例调用。

当然实例方法不仅可以在原型中定义,有以下三种定义方法:

function A(){    this.fun_f=function(){        console.log("Iam in the constructor");    };}A.prototype.fun_p=function(){    console.log("Iam in the prototype");};var a=new A();a.fun_f();//Iam in the constructora.fun_i=function(){    console.log("Iam in the instance");};a.fun_i();//Iam in the instancea.fun_p();//Iam in the prototype

这三种方式的优先级为:直接定义在实例上的变量的优先级要高于定义在“this”上的,而定义在“this”上的又高于 prototype定义的变量。即直接定义在实例上的变量会覆盖定义在“this”上和prototype定义的变量,定义在“this”上的会覆盖prototype定义的变量。

下面看jQuery中extend()方法源码:

jQuery.extend = jQuery.fn.extend = function() {    var options,name, src, copy, copyIsArray, clone,        target= arguments[0] || {},        i =1,        length= arguments.length,        deep= false;    // Handle adeep copy situation    if ( typeoftarget === "boolean" ) {        deep= target;        //Skip the boolean and the target        target= arguments[ i ] || {};        i++;    }    // Handlecase when target is a string or something (possible in deep copy)    if ( typeoftarget !== "object" && !jQuery.isFunction(target) ) {        target= {};    }    // ExtendjQuery itself if only one argument is passed    if ( i ===length ) {        target= this;        i--;    }    for ( ; i< length; i++ ) {        //Only deal with non-null/undefined values        if ((options = arguments[ i ]) != null ) {            //Extend the base object            for( name in options ) {                src= target[ name ];                copy= options[ name ];                //Prevent never-ending loop                if( target === copy ) {                   continue;                }                //Recurse if we're merging plain objects or arrays                if( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray= jQuery.isArray(copy)) ) ) {                   if( copyIsArray ) {                       copyIsArray= false;                       clone= src && jQuery.isArray(src) ? src : [];                   }else {                       clone= src && jQuery.isPlainObject(src) ? src : {};                   }                   //Never move original objects, clone them                   target[name ] = jQuery.extend( deep, clone, copy );                //Don't bring in undefined values                }else if ( copy !== undefined ) {                   target[name ] = copy;                }            }        }    }    // Returnthe modified object    return target;};            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选