Trip
Trip

Reputation: 27114

How can I prevent this class from instantiating?

I am using a jQuery plugin for creating classes. But I don't want my classes to be initialized unless I want them to. How can I prevent the following code from initializing, but be accessible if need be?

window.classes.my_custom_class = new(Class.extend({
 init: function() {
    // stuff()
  }
}));

Upvotes: 0

Views: 612

Answers (1)

Bergi
Bergi

Reputation: 664969

For creating a singleton, you do not use classes at all. Just follow the default module pattern. In your case:

window.classes.my_custom_module = {
    init: function() {
        // stuff()
    }
};

Or did you want an actual "class", i.e. a constructor function? Then avoid that new keyword there!

window.classes.my_custom_class = Class.extend({
    init: function() {
        // stuff()
    }
});

// later:
var instance = new classes.my_custom_class();

Upvotes: 1

Related Questions