Class with optional new keyword [ 778 views ]
Goal: create class allow creation without the new keyword
The pattern is simple:
myClass = function fn( params ) { // here is the trick // without the 'new' keyword the 'this' will be not instance of my class if ( !(this instanceof fn) ) { return new fn( params ); } // put here everything what you want // ... return this; };
The creation itself is the following:
var a = new myClass('a'); // or simple var a = myClass('a');