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');
#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }