0

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!

2
  • 1
    Try to window.SomeClass({}) on your console. Commented Apr 9, 2015 at 16:37
  • It's good to be fairly suspicious of JavaScript (and other web technologies) code samples older than a couple yeas; things change really quickly. Commented Apr 9, 2015 at 16:39

1 Answer 1

1

The code is simply incorrect. It could be fixed however:

this.SomeClass = function SomeClass(options) { // <--- Name the function
    //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
}

By giving the function a name like that, the symbol "SomeClass" will be available inside the function as a reference (to itself).

Note that _doSomething is not defined either, and that the constructor doesn't actually call that function that counts instances.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works now, yeah _doSomething() should be replaced with _incrementInstances(), BTW, is the snippet I posted considered bad practice when writing a JS library and organize the code in a namespace? Or Can I go with it?
@tonix it's not bad, really, but a lot has happened over the past 5 years. In particular, a lot of work has been done on the whole notion of modules for JavaScript (AMD and CommonJS and RequireJS for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.