在网站的开发中,经常会自己写一些jQuery插件来方便使用,其中自然少不了一个关键的方法->jQuery.extend(),使用这个方法来扩展jQuery对象。
那么今天就来讲讲这个函数的实现原理。
这个方法不仅仅可以实现插件,当然也有它本来的功能,扩展对象。like this:
var person = {name:'李四'};
var person2 = $.extend(person,{age:20},{sex:'male'});
//person.name='李四';
//person.age=20;
//person.sex='male';
person2与person扩展后是同一个对象
这个函数的第一个函数指的是要被扩充的对象(target),第二、第三个函数则是附加对象。通俗点将,jQuery将检查target中没有的属性,而在后面两个对象中却存在,那么这些属性将被添加到target中。
那么我们写插件又是怎么回事呢,我们只用了一个参数啊,像这样:
$.extend({
myExtendMethod:function(){
alert('这是我的方法');
}
});
像上面这个例子里,我们的$.extend方法中只有一个Object类型的参数,即:{myExtendMethod},那么这个时候怎么扩展,去扩展谁?target又是什么?当然要扩展的就是我们爱不释手的jQuery了!上一下源码:
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false; // 不会满足,因为我们传进来的是Object
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
} // 同样不会满足
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
} // length为1,i也为1,所以会走这个条件
if ( length === i ) {
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;
}
}
}
} // Return the modified object
return target;
};
通过这段代码可以看到第一行的定义,$.extend和$.fn.extend的定义方法为同一个匿名函数,也就是他们是一个方法实现的,只是扩展的对象前者是jQuery,
后者是jQuery.fn =jQuery.prototype也就是 jQuery实例( $('div') )。$.fn.extend()就是为jQuery添加实例方法。
ps:如果不了解js原型概念可以看我上一篇文章js创建对象,或者看其他园子的文章。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAicAAAD2CAIAAACCz7ApAAAbg0lEQVR4nO3dvU7zTN7H8T0S9wjRICEBEkhIkaC4SbrQbKQoLQUrLR2CcpsIKUv1FFfHrrQFRQq22o4DuNNd55Cj4Cns2DPjebGH8diOvx9dxQXGr0nm5xk7/v8lAQAglr+0vQEAgAEhdQAA8ZA6AIB4SB0AQDykDgAgHlIHABAPqQMAiIfUAQDEQ+oAAOIhdQAA8ZA6AIB4SB0AQDykDgAgHlIHABCPmjqz9fZ7s2xlU/YGxxAATPYpdWbr7XeuxZ3o8zFEAKOn98/P96dR29sBdFLFEbblJm/Ot+tZs5vkabbednXTUj04hoEtNz476jdXh4ye3leLtjcC6KwKqTNbb8VmcrlptSNhNFtvw2zWYvWp86Nz154cw8B6mjo/fQOQOoBNljrCebh6Kq5pBNJfLTfCn2YLEJpSabxLWeLub4u/SWfcbUb6026qs32Wt15dwHa93si/b0Z/j2E6y2YprE5cmXld6m7rf6vZfI+5TPvl2HjLMWwKqQPYaPo6s/VGanI0n9TlZtfCiRPF3sZyIzV0y43ygc/aglJjqPRYapz3Gvo6y43Q1ihb1ZzeHcN0WVL7r2u7lXUpXTj1gpZhzX5zWfbLsvGuY9gEUgew8Uqd/ONvbDHLJ63lM1BD+y8ss9agmTF15AbOvcAQI2y9O4blq2LyLmgXI/+JumZTfvjNpU4SN8i88c5jqPOjN8Bi9flJ5gAWztTRXKQvfmVpMR2fbUtjaGiPXYKlTgi9O4blZcm/6UbqGPbLvPGtXCOirwPYOFNHHQGRhkfE5nS5EUc51IZ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9fNv+SqMfbBJkzrybQf5T/IlFe3GS5u+WWd3JczK08xz6S4/lSImDbu18aYB2+G14R42wGHPU6dRsb6vgwDK/aBGbq/m+zqAC6nzE9G+soOf0SVME6nDswkAJ1IHe0weypNuIDcOGAJoFKkDAIiH1AEAxEPqAADiIXUAAPGQOgCAeEidbqDaaQfxogANIHWaN/1wP/iABi6yBl+U7FkNpe/eXD397z///fM///3z/57+kCYsVp+fumcKdcawa9oiNFInqOmHviEz/b4vgteCi1lcLvaLon/KwOLf//nn39L//vWff/6j9AfdfzbBgKsLIihSJyhSp6EF7roDikpf5e9C6vzx93++XhU//u0f/xJ/NM1l8pOj4Y/UQRikTih50dL839flxW7i9GNyPykeRSoVLTVXO02Sw/uvfIHX92/XFVtJfS3RJEkMRTxtpT/tFUjN9T2F5ez+aFfOwLbAsFp5UXT5ocTMH3//17//6p6rAXtc09bw7HN0DqkTlPm0WryKcPL8u1ReQVeL4eJtUjSFk8vXak/FNtcStRYMNZb+TH821Euz1NwU1iZvkmWBjYj9onQ4dfa6pi2p0xekTlDVBnN0RX30FYCkSg15jQYrc3tuL91mrXimX6i75uZy873dbjVnzt0bYQv3ojSfOj84GsOraYvOIXWCCt3AiQ7vv+RRIL2oqeOIjtl6u91uttvyuWk3+jrNvCj66zpSzPzwuk4gw6hpi84hdYISG7LpR3FVwKuBO7z/kprLUuupH1Iw1xK1FgitnDpyBVJjzU11aM88ZFcefwsr7otivoctv2/t6ul/7dzDtt81bdMeIANs3UfqhDW5fDUXLX1+TIRr0WlrZal2Kl611g7mGAeyjbVEDUU8HaU/5RmrFEKVv+BRa4ENiPqiGPPjj7//68/0+zr5LdQV5gpqr2vaLlZkTk+QOkBYfvnR/e/rdNtiRU28viB1gLBMzyYw6vyzCXpgsSJzeoPUAQDEQ+oAAOIhdQAA8fw/JIJaRLC0VgAAAAAASUVORK5CYII=" alt="" /> 源码:jQuery.fn= jQuery.prototype
下面就来看看$.extend函数只有一个参数时怎么执行的。
看3行代码的target变量,指定了arguments[0]为它的值,按照刚刚我们定义的插件,就是{myExtendMethod:function(){...}}这个object;
继续往下看,看我21行的注释,this指的是jQuery对象,然后把jQuery赋给了target变量,并且把 i 变成了0 ;
然后进入for循环,在第55行代码的时候,会把我们的参数Object赋给jQuery对象,这样,就把myExtendMethod这个方法添加到jQuery对象上了。
$.extend({
testMethod: function () {
alert(111);
}
});
$.fn.extend({
testMethod2: function () {
alert(222);
}
});
//调用扩展方法
$.testMethod();//弹出111
$("#List1").testMethod2();//弹出222