I like to create pretty OO javascript, sometimes with prototype and sometimes more native. In many cases I create javascript classes looking like this
var myObject = myObject || {};
myObject.Example1 = function () {
"use strict";
// ctor
function self() { }
// init
self.init = function () {
console.log("Example 1 did init");
};
return self;
};
myObject.Example2 = (function ($) {
"use strict";
// ctor
function self() { }
// init
self.init = function () {
console.log("Example 2 did init");
};
return self;
})($);
But I seem to have forgot why I can't create an instance of the function enclosed with () and $.
var obj1 = new myObject.Example1();
obj1.init(); // Does init
var obj2 = new myObject.Example2();
obj2.init(); // Does not init (obj2.init is not a function)
The myObject.Example1() works as expected but why can't I call the myObject.Example2()?