Lets say I have a namespace called ns and ns has to functions on it:
ns = {};
ns.Foo = function(message, fn){
this.message = message;
fn.call(this);
};
ns.bar = function() {
alert('Hello, world!');
};
And I call it like this:
var foo = new Foo('My Message', function() {
bar();
});
I get an error saying bar() is undefined. But if I call it this way:
var foo = new Foo('My Message', function() {
this.bar();
});
It works. Is there a way to structure my JavaScript so I can just call bar() without the this?
Thanks!
No. JavaScript is not Java. You need to specify object whose method you’re calling.