javascript module pattern from You don't know JS -


i have been reading , testing below code out several hours , can't seem grasp things. have been stepping through chrome console putting break in every line can add , have been inspecting , not sure of things

1)i not sure of purpose of deps array. first odd thing me , why doesn't script try put data on first call it(from mymodules.define("bar",[],function()) ? why script make second call define(mymodules.define("foo",["bar"], function(bar)) , add ["bar"] array when first define should have done in first place?

2)this code modules[name] = impl.apply(impl,deps). each callbacks, not use 'this'.. apply necessary here? also, lack of understanding in callback when 'apply' used, how 1 read this? thought 'apply' typically goes functionname.apply(obj,[])

in case, saying functionname.apply(functionname, []) ?? or different because function anonymous?

    var mymodules = (function manager() {         var modules = {};          function define(name,deps,impl) {             ( 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:         };     })();      mymodules.define("bar",[],function() {         function hello(who) {             return "let me introduce: " + who;         }          return {             hello : hello         };     })      mymodules.define("foo",["bar"], function(bar) {         var hungry = "hippo";          function awesome() {             console.log(bar.hello(hungry).touppercase() );         }          return {             awesome: awesome         };     });      var bar = mymodules.get("bar");     var foo = mymodules.get("foo");      console.log(bar.hello("hippo"));      foo.awesome(); 

i not sure of purpose of deps array.

you seem confused on purpose of whole mymodules object, don't you?

the define method can used declare module, name, array of dependencies, , factory function:

  • the name string under module object stored in modules dictionary
  • the deps array contains names of modules on declared module depends on.
  • the impl function called create module object available under name. passed module objects names in deps array resolved.

why doesn't script try put data on first call (from mymodules.define("bar",[],function()) ? why script make second call define (mymodules.define("foo",["bar"], function(bar))?

it means declare module name bar without dependencies, , declare module name foo depends on bar. typically, these 2 declarations placed in different scripts.

this code modules[name] = impl.apply(impl,deps) - each callbacks, not use 'this'.. apply necessary here?

yes, apply necessary here pass arbitrary many arguments function. however, passing impl function this value indeed not make sense, null more appropriate here.

a better , more understandable definition be

function define(modulename, dependencynames, factory) {     var dependencies = [];     (var i=0; i<dependencynames.length; i++) {         dependencies[i] = get(dependencynames[i]); // resolve name     }     modules[modulename] = factory.apply(null, dependencies); } 

Comments