码迷,mamicode.com
首页 > 其他好文 > 详细

模块的封装

时间:2015-12-29 21:14:01      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

一:实例化多个(swipphoto之类的,一个页面多个实例)

function index(name){
        this.name = name;
        this.init();//实例化后这个Init自动执行
    }
    index.prototype = {
        init: function(){
            //各种初始化方法
            this.initModel();
        },
        initModel: function(){
            console.log("初始化model");
        },
        getName: function(yourName){
            //对外暴露的方法
          return yourName + this.name;
        }
    }
    var a = new index("sss");
    var b = new index("bbb");
    var newName = a.getName("jjj");
    //indexCtrl后面的写在一个js中,作为一个模块
    var indexCtrl = (function(){
        function index(name){
            this.name = name;
            this.init();
        }
        index.prototype = {
            init: function(){
                //各种初始化方法
                this.initModel();
            },
            initModel: function(){
                console.log("初始化model");
            },
            getName: function(yourName){
                //对外暴露的方法
                return yourName + this.name;
            }
        }
        return index;
    })()
    var a = new indexCtrl("sss");
    var newName = a.getName("jjj");

二:组件式的,baseCtrl后面的单独写在一个Js里,作为模块

var baseCtrl = (function(){
        var base = {
            //各种方法
            fn1:function(para){
                return (para + 10);
            },
            fn2:function(){

            }
        };
        return base;
    })()
    var a = 1;
    a = baseCtrl.fn1(a);

 

模块的封装

标签:

原文地址:http://www.cnblogs.com/darr/p/5086917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!