JS扩展 或 Jquery的扩展写法

<script>
//JS扩展String函数test,其它类推
String.prototype.test = function(s){
alert(this+s);
}
var str = 'hello';
str.test('world');//helloworld

//JQ扩展
(function($){搜索
$.fn.test = function(op){
var defaults = {a:'no'}
var setings = $.extend(defaults,op);
alert(setings.a);
}
})(jQuery);

//调用
$(function(){
$().test();
$().test({a:'yes'});
})
</script>

//JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展
            var aClass = function(){}


            //1 定义这个类的静态方法
            aClass.sayHello = function(){
                alert('say hello');
            }


            //2 定义这个类对象的对象方法
            aClass.prototype.protoSayHello = function(){
                alert('prototype say hello');
            }




            aClass.sayHello() ;//aClass的静态方法


            var aObject = new aClass();
            aObject.protoSayHello();  //类对象的方法扩展


            //JQuery的方法扩展


            //定义jquery的扩展方法
            //1 定义JQuery的静态方法
            jQuery.extend({
                staticHello: function () {
                    alert("wwg, staticHello");
                }
            });


            var str = $.staticHello();


            //2 定义JQuery对象的扩展方法
            jQuery.fn.ObjHello = function () {
                return alert("ObjHello");
            }


            $("#htmlDiv").ObjHello();
原文地址:https://www.cnblogs.com/itjeff/p/3851689.html