var a = 2;
(function foo(){ // <-- 添加这一行
var a = 3;
console.log( a ); // 3
})() // <-- 以及这一行
console.log( a ); // 2
var undefined = true; // 给其他代码挖了一个大坑!绝对不要这样做!
(function IIFE( undefined ) {
var a;
if (a === undefined) {
console.log( "Undefined is safe here!" );
}
})(undefined);
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 );
}
function get(name) {
return modules[name];
}
return {
define: define,
get: get
};
})();
MyModules.define('bar', [], function (){
function hello (who) {
return 'let me instroduce ' + who
}
return {
hello: hello
}
})
let bar = MyModules.get('bar')
console.log(bar.hello('ash'))
console