FIG cubs cut self-cultivation - widget modules of notation

Preface:

Roughly remember thinking about writing module plug-in of


General writing:


define(function(require, exports, module) {

        var plugin = {
            
            //插件初始化入口
            init : function(options){
                var that=this;
                this.parameter1   = options.parameter1 || {};  //获取传入参数
                this.parameter2   = options.parameter2 || {};   //获取传入参数
                this.fun1  = options.fun1 || function(){};  //获取传入参数
                this.fun2 = options.fun2 || function(){};  //获取传入参数
                this.method1();
                ...
            }, 
            
            //插件公用方法1
            method1:function(){
                var that=this;
                ...
            },
    
            //插件公用方法2
            method2:function(){
                var that=this;
                ...
            },
                        
            ...
    
    }
        
        plugin.init(); //初始化插件
        module.exports = plugin;  //暴露模块/方法
    });

Invocation:

var plugin = require('./plugin'); //引用模块插件
plugin.method1();                 //调用插件公有方法

Precautions:

  • When the plug-in initialization, if there is a variable 缺省值, usually with ||logic

  • Within the plug-in 私有变量, 私有方法all underlined to identify, for example, _parameter1indicate private variables, _fun1(){}tables private methods

  • Always bear in mind 单一职责原则, only a method of performing a basic logic, if there are multiple logical, you need to add other methods to the basic composition of pelletized, which can reduce the complexity, time ensure that the entire insert 易读性and可扩展性

Guess you like

Origin www.cnblogs.com/10manongit/p/12627352.html