jQuery 的方法扩展,$.extend()、$.fn.extend()和$.fn区别

$.extend(object)

是在jQuery的 类 上扩展,如 $.ajax();

$.fn.extend(object)

是在jQuery的 实例 上扩展,也就是在 jQuery.prototype 上添加 成员函数, 如:

$.prototype.method = function(){};

$.fn 和 $.fn.extend(object) 都是添加 成员函数,是写法不一样

代码:

(function($){
    var fun = function(){
        return {
            aa: function(){
                console.log(1)
            }
        }
    };
    $.fn.extend(fun());
    $().aa();         //输出 1

    //覆盖了 aa()的函数
    $.fn.aa = function () {
        console.log(11);
    };
    $().aa();    //输出 11
})(jQuery);

$() 就是一个 jQuery 的实例;

参考:http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html

原文地址:https://www.cnblogs.com/damade/p/4432757.html