Here's an example from YDKJS Scope & Closures, with a couple of slight modifications:
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];
}
function printMod() {
console.log(modules);
}
return {
define, get, printMod
}
})();
MyModules.define("bar", [], function() {
function hello(who) {
return "Let me introduce: " + who;
}
return { hello };
});
MyModules.define("foo", ["bar"], function(b) { // What is passing an object into b here?
console.log(b);
var hungry = "hippo";
function awesome() {
console.log(b.hello(hungry).toUpperCase());
}
return { awesome };
});
var bar = MyModules.get("bar");
var foo = MyModules.get("foo");
I've analyzed this snippet and understand what's going on. The one question I have however is how does the parameter in the pointed out line get its value of an object with the hello function reference? Shouldn't b be getting assigned the deps array we've supplied in the line modules[name] = impl.apply(impl, deps); ? Can someone help me sort out this confusion? I've never really used the apply method before.