zoukankan      html  css  js  c++  java
  • jquery插件编写模版

    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);
        }
    })();
    

      

     
     
  • 相关阅读:
    Eclipse程序员要掌握的常用快捷键
    HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】
    nyoj1032——Save Princess——————【set应用】
    nyoj1087——摆格子——————【规律题】
    hrb——开锁魔法I——————【规律】
    ACdream 1099——瑶瑶的第K大——————【快排舍半,输入外挂】
    ACdream 1098——圆有点挤——————【数学计算】
    FZU 1884——排火车——————【栈的模拟】
    FZU 1921——栀子花开——————【线段树单点更新】
    FZU 1924——死锁——————【topo判环】
  • 原文地址:https://www.cnblogs.com/gxrsprite/p/4172467.html
Copyright © 2011-2022 走看看