javascript插件的几种写法

一、js原生插件的写法

(1)工厂模式

var Helloword = function(objId){
    var _get_dom = function(Id){
        return document.getElementById(Id);
    }
    var _aim_obj = _get_dom(objId);
    var _say_hello = function(str){
        _aim_obj.innerHTML = str;    HelloWorld.sayHello('hello','hello text');
    }
    return{
        sayHello:_say_hello
    }
}

var Hei = new Helloword('hello');
Hei.sayHello('Hello Word');

var Hei2 = new Helloword('hi');
Hei2.sayHello('hi');

由调用我们可以看出,每调用一次就要重新示例化一次,这样次数少了还好,加入一百次,一千次,占用内存不说,还相当繁琐,如此我们应该想到让内存中保存一份就够了

(2)自执行函数

;
var plugin =(function(){
    function _firstFunc(str){
        console.log(str);
    };
    return{
        firstFunc: _firstFunc,
    };
})();

 <script type="text/javascript">
        plugin.firstFunc("Hello ! I am firstFunc");
    </script>

这样看起来很方便,当然也是我很喜欢的一种插件写法


(3)面向对象,prototype原型模式

//自定义类    
function plugin(){}

//提供默认参数
plugin.prototype.str = "default param";

//提供方法(如果不传参,则使用默认参数)
plugin.prototype.firstFunc = function(str = this.str){
    alert(str);
}

//创建"对象"
var p = new plugin();
//调用方法
p.firstFunc("Hello ! I am firstFunc");//Hello ! I am firstFunc
p.firstFunc();//default param


二、jQuery插件写法

(1)对JQuery自身的扩展插件

这种插件是对JQuery自身的方法库进行扩展的。在使用的时候通过$.MethodName()的方式直接使用。

$.extend({
    handleTableUI: function(table){
        varthisTable = $("#" +table);
        
        $(thisTable).find("tr").bind("mouseover",function () {
            $(this).css({color: "#ff0011", background:"blue" });
        });
        $(thisTable).find("tr").bind("mouseout",function () {
            $(this).css({color: "#000000", background:"white" });
        });
    }
});


<scripttype="text/javascript">
    $(document).ready(function() {
        $.handleTableUI("newTable");
    });
</script>

(2)对HTML标记或页面元素进行扩展
使用这种插件的扩展方式,在使用此插件时,需要首先引用经过JQuery包装的页面元素,如:$("#tableId").Method()。

(function ($) {
    $.fn.setTableUI= function(options){
        var defaults = {
            evenRowClass:"evenRow",
            oddRowClass:"oddRow",
            activeRowClass:"activeRow"
        }
        var options = $.extend(defaults, options);
        this.each(function(){
            varthisTable=$(this);
            $(thisTable).find("tr").bind("mouseover",function () {
                $(this).css({color: "#ff0011", background:"blue" });
            });
            $(thisTable).find("tr").bind("mouseout",function () {
                $(this).css({color: "#000000", background:"white" });
            });
        });
    };
})(jQuery);

<script type="text/javascript">
    $(document).ready(function() {
        $("#newTable").setTableUI();
    });
</script>

(3) 对HTML标记或页面元素进行扩展

不要用在页面显式调用JQuery的方法,而是通过直接添加JQuery插件脚本引用,即可实现对该插件的调用。

一般,如果需要用到一些全局的JQuery插件,即:插件的方法不需要显式调用,而是引用脚本即可;同时,这种插件一般对整个Web页面起到全局配置或设置的作用,如:对<body></body>内部的内容进行整体布局,此时可以采用脚本引用的方式实现。
插件代码示例:
(function ($) {
    $.tableUI= { set: function (){
        varthisTable = $("table");
        $(thisTable).find("tr").bind("mouseover",function () {
            $(this).css({color: "#ff0011", background:"blue" });
        });
        $(thisTable).find("tr").bind("mouseout",function () {
            $(this).css({color: "#000000", background:"white" });
        });
    }
    };
    //此处需要进行自调用
    $(function() {
        $.tableUI.set();
    });
})(jQuery);

示例说明:如果上面这段代码在my.plugin.js文件中,那么,我们只需要在页面上添加对此脚本文件的引用即可,引用方式为: <scriptsrc="Scripts/my.plugin.js"type="text/javascript"></script>,当然,在所有要用到JQuery的地方,需要首先添加对JQuery库脚本的引用。在引用型插件的代码中,最主要的就是在插件中要主动调用自己所写的插件方法,上面代码中有注释的地方。否则,我们写的插件代码将不会起作用。


特别提醒,该博文有些代码以及说明来自大神博文,说声抱歉,在这里更多的是对于插件的一种整合与记忆。













猜你喜欢

转载自blog.csdn.net/qq_25065257/article/details/74690859