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) "htmlcode">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。感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 《黑神话》纪录片9月20日上线!揭秘关卡为何删减
- 游戏科学估值已和育碧市值相当:后者有两万多名员工
- 黑神话第81周目Boss伤害有多恐怖?起手伤害直接秒
- 群星.2009-原班情歌2CD【SONY】【WAV+CUE】
- 群星.2024-长乐曲电视剧原声带【TME影音】【FLAC分轨】
- 群星.1993-烧得厉害VOL.1好戏好歌唱不停【滚石】【WAV+CUE】
- 那英《幸福的泪 磁性和爆发力完美展现 2CD》[WAV/分轨][600MB]
- 《宝丽金辉煌60年 女人篇3CD》[WAV+CUE][1.5G]
- 《年度十大发烧唱片精选 唱片榜获奖作品 2CD》[WAV/分轨][1GB]
- 归龙潮三枚天灯碎片位置在哪 笼律鸟任务天灯图文位置
- 归龙潮新手开荒阵容怎么搭配 新手前期阵容养成攻略
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]