I have taken this snippet from http://frugalcoder.us/post/2010/02/11/js-classes.aspx:
if (typeof My == 'undefined')
My = {};
if (typeof My.Namespace == 'undefined')
My.Namespace = {};
//begin private closure
(function(){
//this is a private static member that is only available in this closure
var instances = 0;
//this is a private static method that can be used internally
function _incrementInstances() {
instances++;
}
//Define SomeClass (js uses functions as class constructors, utilized with the "new" keyword)
this.SomeClass = function(options) {
//if the function is called directly, return an instance of SomeClass
if (!(this instanceof SomeClass))
return new SomeClass(options);
//call static method
_doSomething();
//handle the options initialization here
}
//create a public static method for SomeClass
this.SomeClass.getInstanceCount = function() {
return instances; //returns the private static member value
}
//create an instance method for SomeClass
this.SomeClass.prototype.doSomething = function() {
/*Do Something Here*/
}
//end private closure then run the closure, localized to My.Namespace
}).call(My.Namespace);
Then, inside the document.ready callback, both:
$(function () {
My.Namespace.SomeClass({});
});
and:
$(function () {
new My.Namespace.SomeClass({});
});
Give the following error:
Uncaught ReferenceError: SomeClass is not defined
What am I missing? I guess maybe it is because the tutorial is old (2010)?
Thanks in advance!
window.SomeClass({})on your console.