0

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()
  }
}));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Trip
  • 26,756
  • 46
  • 158
  • 277
  • 2
    by looking at the plugin the only way is to either modify the plug in, or don't use init – Ibu Mar 25 '13 at 19:22
  • Is there any difference between [that gist](https://gist.github.com/gotoAndBliss/5239816) and the [original script from this article](http://ejohn.org/blog/simple-javascript-inheritance/)? – Bergi Mar 25 '13 at 19:35
  • And no, it's no jQuery plugin!!! – Bergi Mar 25 '13 at 19:36

1 Answers1

1

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();
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375