jquery外掛程式編寫模版

來源:互聯網
上載者:User

標籤:blog   ar   io   sp   java   on   資料   div   log   

jquery外掛程式是什嗎??這裡以討論實力方法為主,比如 $("div").pluginname({});

他的最簡單形勢應該是

$.prototype.plugin = function(){}


我們一點點來加東西

1,自己的變數不汙染全域,別人的變數不汙染我們

(function($,undefined){var window = Function("return this")();//一定是window$.prototype.plugin = function(){ } )(jQuery)

  

2,判斷已載入或者處理其他附加資料,處理參數,執行個體化一個對象,執行個體化的方式不一定要new一個,你喜歡也可以拷貝一個,工廠一個等。。

        function plugin(element, options) {            var self = this;            self.element = element;            self.$element = $(element);            if (typeof options == "object") {                self.opts = $.extend({}, defaults, options);            }        }        $.fn[pluginName] = function (options, callback) {            var dataname = "plugin_" + pluginName;            $(this).each(function (index, item) {                var hasObject = $(item).data(dataname);                if (!hasObject) {                    var someobj = new plugin(item, options);                    $(item).data(dataname, someobj);                }            });        }        return $.fn[pluginName];

  3,AMD CMD載入

    // 全域模式     var pluginobj = factory(jQuery);    //UMD    if (typeof exports === ‘object‘) {        module.exports = pluginobj;    }    //AMD    if (typeof define === "function" && define.amd) {        define(pluginName, ["jquery"], function () {            return pluginobj;        });    }    //CMD    if (typeof define === "function" && define.cmd) {        define(pluginName, [‘jquery‘], function (require, exports, module) {            var $ = require(‘jQuery‘);            module.exports = pluginobj;        });    }

  全域模式是否開放取決於你的依賴項是否必然載入。

此時把原來的自執行外面再套一層,把原來的自執行改成普通方法改名為factory方法。

 
完整版:
(function () {    var window = Function("return this")();    var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../..";    var pluginName = "mypluginname";    function factory($) {        "use strict";        if ($.isFunction($.fn[pluginName])) {            return $.fn[pluginName];        }        function plugin(element, options) {            var self = this;            self.element = element;            self.$element = $(element);            if (typeof options == "object") {                self.opts = $.extend({}, defaults, options);            }        }        $.fn[pluginName] = function (options, callback) {            //something like old jquery plugin            var dataname = "plugin_" + pluginName;            $(this).each(function (index, item) {                var hasObject = $(item).data(dataname);                if (!hasObject) {                    var someobj = new plugin(item, options);                    $(item).data(dataname, someobj);                }            });        }        return $.fn[pluginName];;    }    var loaded = false;    //UMD    if (typeof exports === ‘object‘) {        module.exports = factory();    }    //AMD    if (typeof define === "function" && define.amd) {        define(pluginName, ["jquery"], factory);        loaded = true;    }    //CMD    if (typeof define === "function" && define.cmd) {        define(pluginName, [‘jquery‘], function (require, exports, module) {            var $ = require(‘jQuery‘);            module.exports = factory($);        });    }    // other    if (!jQuery.xxx && jQuery.loadScript) {        $.loadScript("/scripts/plugins/xxx.js", function () {            pluginobj = factory(jQuery);        })    }    if (!loaded) {        // 全域模式 也可以不判斷強制載入是執行一遍全域模式        factory(jQuery);    }})();

  

 
 

jquery外掛程式編寫模版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.