zoukankan      html  css  js  c++  java
  • JS模块函数

    var MyModules = (function Manager() {
    var modules={};
    // 变量名 数组 回调函数
    function define(name, deps, impl) {
    for (var i=0; i<deps.length; i++) {
    deps[i] = modules[deps[i]];
    }
    modules[name] = impl.apply( impl, deps );
    //apply第一个参数和调用者一样,可以直接传null == modules[name] = impl.apply( null, deps );
    }
    function get(name) {
    return modules[name];
    }
    return {
    define: define, //设置modules的值
    get: get //获得modules的值
    };
    })();

    MyModules.define( "bar", [], function() {
    function hello(who) {
    return "Let me introduce: " + who;
    }
    return {
    hello: hello
    };
    } );
    //上面执行后等于改变了modules的值如下:
    // modules = {
    // "bar":{
    // "hello":function hello(who) {
    // return "Let me introduce: " + who;
    // }
    // }
    // }

    MyModules.define( "foo", ["bar"], function(bar) {
    var hungry = "hippo";
    function awesome() {
    console.log( bar.hello( hungry ).toUpperCase() );
    }
    return {
    awesome: awesome
    };
    } );
    //上面执行后等于改变了modules的值如下:
    // modules = {
    // "foo":{
    // "awesome":function(){
    // console.log( { "hello":function hello(who) { return "Let me introduce: " + who;} }.hello( hungry ).toUpperCase() );
    // }
    // }
    // }
    var bar = MyModules.get( "bar" );
    var foo = MyModules.get( "foo" );
    console.log( bar.hello("hippo") ); // Let me introduce: hippo
    foo.awesome(); // LET ME INTRODUCE: HIPPO
  • 相关阅读:
    20200902
    20200808
    20200801
    20191017
    LeetCode #974. Subarray Sums Divisible by K 数组
    LeetCode #532. K-diff Pairs in an Array 数组 哈希 双指针
    LeetCode #234. Palindrome Linked List 链表 栈 链表逆置
    LeetCode #307. Range Sum Query
    LeetCode #45. Jump Game II 数组 贪心
    LeetCode #55. Jump Game 数组 贪心 线性DP 回溯
  • 原文地址:https://www.cnblogs.com/Jasonellen/p/5866085.html
Copyright © 2011-2022 走看看