欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery extend()详解及简单实例

程序员文章站 2022-07-20 09:21:40
jquery extend()详解及简单实例 使用jquery的时候会发现,jquery中有的函数是这样使用的: $.get(); $.post();...

jquery extend()详解及简单实例

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

$.get(); 
$.post(); 
$.getjson(); 

有些函数是这样使用的:

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

有些函数是这样使用的:

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

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

functiona(){ 
     
  } 
  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 function 
  a.fun_c();//constructor 
  a.fun_p();//prototpye 
  a.fun_c();//a.fun_c is not a function 

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

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

functiona(){ 
    this.fun_f=function(){ 
        console.log("iam in the constructor"); 
    }; 
} 
a.prototype.fun_p=function(){ 
    console.log("iam in the prototype"); 
}; 
var a=newa(); 
a.fun_f();//iam in the constructor 
a.fun_i=function(){ 
    console.log("iam in the instance"); 
}; 
a.fun_i();//iam in the instance 
a.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; 
}; 

(1)首先,jquery和其原型中extend()方法的实现使用的同一个函数。

(2)当extend()中只有一个参数的时候,是为jquery对象添加插件。在jquery上扩展的叫做工具方法,在jquery.fn(jquery原型)中扩展的是实例方法,即使在jquery和原型上扩展相同名字的函数也可以,使用jquery对象会调用工具方法,使用jquery()会调用实例方法。

(3)当extend()中有多个参数时,后面的参数都扩展到第一个参数上。

var a={}; 
$.extend(a,{name:"hello"},{age:10}); 
console.log(a);//object{name: "hello", age: 10} 

(4)浅拷贝(默认):   

var a={}; 
varb={name:"hello"}; 
$.extend(a,b); 
console.log(a);//object{name: "hello"} 
a.name="hi"; 
console.log(b);//object{name: "hello"} 

b不受a影响,但是如果b中一个属性为对象:

var a={}; 
varb={name:{age:10}}; 
$.extend(a,b); 
console.log(a.name);//object{age: 10} 
a.name.age=20; 
console.log(b.name);//object{age: 20} 

由于浅拷贝无法完成,则b.name会受到a的影响,这时我们往往希望深拷贝。

深拷贝:   

var a={}; 
varb={name:{age:10}}; 
$.extend(true,a,b); 
console.log(a.name);//object{age: 10} 
a.name.age=20; 
console.log(b.name);//object{age: 10} 

b.name不受a的影响。

 var a={name:{job:"web develop"}}; 
var b={name:{age:10}}; 
$.extend(true,a,b); 
console.log(a.name);//age: 10 job: "web develop" 
  //b.name没有覆盖a.name.job。 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!