Query开发插件的两个方法

1.jQuery.extend(object);为扩展jQuery类本身.为类添加新的方法。
2.jQuery.fn.extend(object);给jQuery对象添加方法。

jQuery.fn
1
2
3
4
5
jQuery.fn = jQuery.prototype = {
init: function(selector, context) {
//内容
}
}

3.jQuery.fn = jQuery.prototype。

4.虽然 JavaScript 没有明确的类的概念,但是用类来理解它,会更方便。jQuery便是一个封装得非常好的类,比如我们用 语句$(“#div1”)会生成一个 jQuery类的实例。

jQuery.extend(object)
1
2
3
4
5
6
7
8
9
10
jQuery.extend({
min: function(a, b) {
return a < b ? a : b;
},
max: function(a, b) {
return a > b ? a : b;
}
});
jQuery.min(2, 3); // 2
jQuery.max(4, 5); // 5
jQuery.fn.extend(object);

就是为jQuery类添加“成员函数”。jQuery类的实例才可以调用这个“成员函数”。

栗子②

比如我们要开发一个插件,做一个特殊的编辑框,当它被点击时,便alert 当前编辑框里的内容。可以这么做:

1
2
3
4
5
6
7
8
9
$.fn.extend({
alertWhileClick: function() {
$(this).click(function() {
alert($(this).val());
});
}
});
//$("#input1")是jQuery的实例,调用这个扩展方法
$("#input1").alertWhileClick();

jQuery.extend() 的调用并不会把方法扩展到对象的实例上,引用它的方法也需要通过jQuery类来实现,如jQuery.init()

jQuery.fn.extend()的调用把方法扩展到了对象的prototype上,所以实例化一个jQuery对象的时候,它就具有了这些方法,在jQuery.JS中到处体现这一点
jQuery.fn.extend = jQuery.prototype.extend
你可以拓展一个对象到jQuery的 prototype里去,这样的话就是插件机制了。

1
2
3
4
5
6
7
8
(function($) {
$.fn.tooltip = function(options) {};
//等价于 var
tooltip = {
function(options) {}
};
$.fn.extend(tooltip) = $.prototype.extend(tooltip) = $.fn.tooltip
})(jQuery);