zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    CSS印象不深的小地方
    gulp常用插件的使用
    移动端手势库Hammer.js—增强touch事件或手势
    HTML5拖放与文件操作api,实现拖拽上传文件功能
    Less相关
    gulp使用(一)
    将博客搬至CSDN
    jquery Ajax 通过jsonp的方式跨域提交表单
    解决“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”问题
    使用eclipse4.5创建maven项目
  • 原文地址:https://www.cnblogs.com/itjeff/p/3851689.html
Copyright © 2011-2022 走看看