How do you add objects to a javascript namespace?
Posted
by Fletcher Moore
on Stack Overflow
See other posts from Stack Overflow
or by Fletcher Moore
Published on 2010-06-16T19:23:20Z
Indexed on
2010/06/16
19:32 UTC
Read the original article
Hit count: 236
var Test = (function() {
return {
useSub: function () {
this.Sub.sayHi();
},
init: function () {
$(document).ready(this.useSub);
}
};
})();
Test.Sub = (function () {
return {
sayHi: function () {
alert('hi');
}
};
})();
Test.useSub(); // works
Test.init(); // explodes
Above I am trying to create a Test namespace and add an object Sub to it. I was doing fine until I tried using the object in jQuery. The error is "Uncaught TypeError: Cannot call method 'sayHi' of undefined". If there is a better way to do this, I am open to it.
© Stack Overflow or respective owner