Skip to main content
Source Link
fmsf
  • 207
  • 1
  • 7

JavaScript static methods retrieval

I have been thinking about it and wanted some feedback, recently I thought about doing it like this:

function foo(){ if ( !foo.prototype.statics ){ foo.prototype.statics = { // declare static functions/vars here } } if ( !(this instanceof foo) ){ return foo.prototype.statics; } var statics = foo.prototype.statics; // shortcut // if it reaches here then it's an instance (new foo()) } foo().callStaticMethod(); (new Foo()).callInstanceMethod(); 

Is this a good way to do it or do you think of any reason why this can be an anti-patern?