I have the following structure to my JS:
(function () {
this.Example = {
init: function () {
var self = this;
var self2 = Example;
$(document).ready(function () {
// THIS WORKS
Example.function1();
// THIS DOES NOT WORK
self.function1();
// THIS DOES NOT WORK EITHER
self2.function1();
});
console.log('init');
}(),
function1: function () {
console.log('function1');
},
function2: function () {
// THIS WORKS
Example.function1();
// THIS ALSO WORKS
this.function1();
// THIS ALSO WORKS
var self = this;
self.function1();
console.log('function2');
}
}
})();
What I'm finding is that when I call a function inside my object literal using this
or declaring it on a variable or direct using Example
it works fine.
But it in my init()
I MUST call it using the literal name and using either this
or declaring a variable outside of the document ready still causes errors. Even if declare the variable as var self = Theme
it doesn't work and I must call it directly.
Why is this?