ת ¶ÁjQuery£¨Á½ÖÖÀ©Õ¹£©
ÅÖÒô·û ·¢±íÓÚ 6ÌìÇ° (2015-11-09 17:24:50) | ÆÀÂÛ£¨0£© | ÔĶÁ´ÎÊý£¨37£©| 0 ÈËÊղشËÎÄÕÂ,
ÉÏһƪ·ÖÎöÁËjQuery¶ÔÏóµÄ×é³É£¬Õâƪ·ÖÎöÏÂËüµÄextend·½·¨¡£ÈçÏÂ
1
2
3
jQuery.extend = jQuery.fn.extend = function() {
...
};
ÎÒÃÇ¿ÉÒÔÓÃ$.extendÈ¥À©Õ¹×Ô¶¨ÒåµÄ¶ÔÏó£¬Èç
1
2
3
var myself = {name:jack};
$.extend(myself, {setName: function(n){this.name=n;} });
myself.setName("tom");
ͨ¹ý.extendΪ¶ÔÏómyselfÌí¼ÓÁËsetName·½·¨¡£µ«ÕâÀïÖ÷ÒªÌÖÂÛ.extendÈçºÎ¹¹½¨jQuery¿âµÄ¡£²»Öª×¢Òâµ½ÉÏÃæ´úÂëÖÐjQuery.extendºÍjQuery.fn.extendÊÇͬһ¸öº¯Êý¡£ÎÒÃÇä¯ÀÀÏÂjQuery¿â£¬·¢ÏÖÓÐЩ·½·¨ÊÇͨ¹ýµ÷ÓÃjQuery.extendÀ©Õ¹µÄ£¬ÓÐЩÔòÊÇͨ¹ýµ÷ÓÃjQuery.fn.extendÀ©Õ¹µÄ¡£
ÏÂÃæ·Ö±ðÌÖÂÛ£º
1£¬Í¨¹ýjQuery.extendÀ©Õ¹
ÎÒÃÇÖªµÀjQuery.extendÖеÄjQueryÀàÐÍÊÇfunction£¬¼´typeof jQueryֵΪ×Ö·û´®¡°function¡±¡£Èç¹û°ÑjQueryµ±³ÉÒ»¸öÀ࣬jQuery.extendÏ൱ÓÚΪ¸ÃÀàÌí¼ÓÁ˾²Ì¬·½·¨extend¡£¾²Ì¬·½·¨ÊDz»ÐèÒªnewÒ»¸öʵÀýÔÙÈ¥µ÷Óõģ¬Í¨¹ý¡°ÀàÃû+·½·¨Ãû¡±Ö±½Óµ÷Ó᣼´jQuery.extend(...)£¬jQueryÓÖ±»¸³Öµ¸ø¡£Òò´ËÎÒÃǸüÏ°¹ß.extend(...)¡£
Ô´ÂëÖÐÖ±½Óµ÷ÓÃjQuery.extend·½·¨£¬Ö»´«Ò»¸ö²ÎÊý¡£ÈçÏÂ
1
2
3
4
5
6
7
8
9
jQuery.extend({
noConflict: function( deep ) {
window.$ = _$;
if ( deep )
window.jQuery = _jQuery;
return jQuery;
},
...
});
ÎÒÃÇÖªµÀextendÖÐÈç¹ûÖ»´«Ò»¸ö²ÎÊý£¬ÄÇô½«Ö´Ðиþä
1
2
3
4
if ( length === i ) {
target = this;
--i;
}
¼´À©Õ¹×Ô¼º£¬¶øÕâÀïµÄthisÔòÊÇfunction jQuery¡£Ò²¾ÍÊÇ˵¸øfunction jQueryÌí¼ÓÁËÐí¶à¾²Ì¬·½·¨£¬ÕâЩ·½·¨¶¼¿ÉÒÔÖ±½Óͨ¹ýjQuery.xx(»ò.xx)·½Ê½À´µ÷Ó㬶ø²»ÊÇÏÈÖ´ÐÐ(µ÷ÓÃ)jQuery·½·¨ÔÙÈ¥µ÷ÓÃxx£¬Èç("#id").xx¡£Ò²ÐíÏÂÃæÕâ¸öÀý×Ó¸üÈÝÒ×Àí½â
1
2
3
4
5
6
7
8
9
10
11
12
13
function fun(){}//¶¨ÒåÒ»¸öÀࣨº¯Êý£©
//Ϊ¸ÃÀࣨº¯Êý£©Ìí¼ÓÒ»¸ö¾²Ì¬·½·¨extend
fun.extend=function(obj){
for(var a in obj)
this[a] = obj[a];//×¢Ò⣺ÕâÀïµÄtihs¼´fun
}
//µ÷ÓÃextendΪ¸ÃÀàÌí¼ÓÁ˾²Ì¬ÊôÐÔname£¬¾²Ì¬·½·¨method1
fun.extend({name:"fun",method1:function(){}});
//Êä³öname,prototype,extend,method1
console.dir(fun)
Òò´Ë£¬jQueryÖеÄisFunction, isArray, isWindowµÈ¶¼ÊǾ²Ì¬·½·¨£¬Ö»ÄÜͨ¹ý.isFunction,.isArray, .WindowÒýÓ᣶ø²»ÄÜͨ¹ý("...").isFuction, ("...").isArray,("...").isWindow·½Ê½ÒýÓá£
2£¬Í¨¹ýjQuery.fn.extendÀ©Õ¹
jQuery.fnµÈÓÚjQuery.prototype£¬Ò²¾ÍÊÇ˵¸øfunction jQueryµÄÔÐÍ(prototype)ÉϹÒÁ˸öextend·½·¨¡£Í¨¹ýµ÷ÓÃjQuery.fn.extend(object)À´À©Õ¹Ê±(×¢ÒâÖ»´«Ò»¸ö²ÎÊýobject)£¬extendº¯ÊýÖÐÈÔÈ»»áÖ´ÐÐ
1
2
3
4
if ( length === i ) {
target = this;
--i;
}
¶øÕâʱµÄthisÔòÊÇjQuery.prototype£¨µÚÒ»ÌõÌáµ½µÄÔòÊÇjQueryº¯Êý×ÔÉí£©¡£¼´¸øjQuery.prototypeÉÏÌí¼ÓÁËÐí¶àÊôÐÔ£¬·½·¨¡£µ±jQueryº¯ÊýÖ´ÐÐʱ£¬Èç()»òjQuery()£¬¸ü¶àʱºòÊÇ()¡£¸Ãº¯Êý»ánewÒ»¸öjQuery£¨¼ûÉÏһƪjQuery¶ÔÏóµÄ×é³É£©¡£ÕâʱÔò°ÑÀ©Õ¹µÄÊôÐÔ£¬·½·¨¶¼¸½¼Óµ½newÉú³ÉµÄ¶ÔÏóÉÏÁË¡£Ò²ÐíÏÂÃæÕâ¸öʾÀý¸üÈÝÒ×Àí½â
1
2
3
4
5
6
7
8
9
10
11
12
13
function fun(){}//¶¨ÒåÒ»¸öÀࣨº¯Êý£©
//¸ø¸ÃÀàÔÐÍÉÏÌí¼ÓÒ»¸ö·½·¨extned
fun.prototype.extend = function(obj){
for(var a in obj)
this[a] = obj[a];//×¢Ò⣺ÕâÀïµÄthis¼´ÊÇfun.prototype
}
//µ÷ÓÃextend·½·¨¸øfun.prototypeÉÏÌí¼ÓÊôÐÔ£¬·½·¨
fun.prototype.extend({name:"fun2",method1:function(){}})
//Êä³öname,extend,method1
console.dir(new fun())
¡¡