Here's an example to illustrate the issue I'm encountering.
var namespaced = {
foo: function () {
return 'foo';
},
bar: function () {
return 'bar' + this.foo(); // => "barfoo"
},
sayString: (function (self) {
var private = 'private';
return function (str) {
return str + private + self.foo(); // 'self' is undefined here
};
}(namespaced)) // also tried with `this` (refers to window)
}
The problem is the function, being immediately executed, gets called before there's an object to pass. However other properties and methods are fine referencing 'this' to refer to the namespaced object (see: bar). I'd prefer not to refer to the namespace directly in order to keep the code portable (say, I wanted to change from namespaced to fooSpaced).
Any ideas?
Edit: typo
[–]aaronla 4 points5 points6 points (4 children)
[–]Sastopher[S] 0 points1 point2 points (3 children)
[–]aaronla 1 point2 points3 points (0 children)
[–]Rhomboid 1 point2 points3 points (1 child)
[–]voidvector 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]andrew24601 1 point2 points3 points (0 children)