码迷,mamicode.com
首页 > 编程语言 > 详细

js模块化 javascript 模块化 闭包写法 闭包模块化写法

时间:2018-12-10 13:56:17      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:divide   world   ide   src   ||   function   inf   rac   ota   

    var main = main || {};

    ; (function (main) {
        ‘use strict‘;

        //私有变量
        var _s1 = ‘Hello ‘;
        var _s2 = ‘World!~‘;

        //私有方法
        var _func = {
            helloWorld: function (str1, str2) {
                return str1 + str2;
            }
        };

        //公有方法
        main.method = {
            add: function (a, b) {
                return a + b;
            },
            subtract: function (a, b) {
                return a - b;
            },
            multiply: function (a, b) {
                return a * b;
            },
            divide: function (a, b) {
                return a / b;
            },
            total: function (a, b) {
                return _func.helloWorld(_s1, _s2) + this.add(a, b) + this.subtract(a, b) + this.multiply(a, b) + this.divide(a, b);
            }
        };

        //将公有方法返回
        return main.method;
        
    })(main);

    var t = main.method.total(1, 1);
    console.log(t);///"Hello World!~2011"

 实例:

技术分享图片

技术分享图片

技术分享图片

js模块化 javascript 模块化 闭包写法 闭包模块化写法

标签:divide   world   ide   src   ||   function   inf   rac   ota   

原文地址:https://www.cnblogs.com/eedc/p/10095733.html

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