jQuery 别名 $
一. 类级别扩展方法(好比$.ajax(...))python
1> 单个全局方法ajax
$.testExtend = function (){ console.log("单个全局方法"); }
# 调用:$.testExtend();
2> 多方法继承 this
$.extend({ func01 : function(){ console.log("func01"); }, func02 : function(){ console.log("func02"); }, func03 : function(){ console.log("func03"); } })
# 调用:$.func01()/$.func02()/$.func03()
3> 自定义命名空间(我的理解为类,上面两种的结合) spa
$.space = { func01 : function(){ console.log("func01"); }, func02 : function(){ console.log("func02"); }, func03 : function(){ console.log("func03"); } }
# 调用: $.space.func01();
二. 对象下扩展方法对象
1> 最简单的一种blog
$.fn.func01 = function(){ console.log("func01"); }
# 调用: $("#btn").func01();
2> 多方法继承
(function($){ $.fn.extend({ testing:function(opt,callback){ console.log("testing"); }, testing1:function(opt,callback){ console.log("testing1"); } }) })(jQuery);
# 调用:$("#btn").testing($(this),function(){});
(function (ee) { ee.fn.f1 = function (obj) { alert(obj+"f1"); }; ee.fn.f2 = function (obj) { alert(obj+"f2"); }; })(jQuery)
# 调用:$("#btn").f1("hello");