javascript - Calling member methods before object declaration is finished -
this question has answer here:
sometime ago heard practice wrap code in big object serve namespace in order reduce global namespace cluttering , facilitate library export, tried this.
var wrapper = { foo: function(){ return 42; }, bar: this.foo() };
it fails, claiming "foo not defined". calling methods before finishing object declaration bad, moved bar, , worked.
var wrapper = { foo: function(){ return 42; }, }; wrapper.bar = wrapper.foo();
i feel can become kind of ugly, nested namespaces , such, there workarounds don't make hard see of wrapper's members @ once?
the problem this
going equal global context. need access function so:
var wrapper = { foo: function(){ return 42; }, bar: null, init : function() { // initialization code goes here wrapper.bar = wrapper.foo(); } }; wrapper.init();
this method great organizing code logical chunks , future developers can find looking in javascript.
Comments
Post a Comment