What is the best way to allow the usage of static methods while restricting the use of instance functions without utilizing the new operator? In this scenario, the constructor will trigger an exception if it is called without the new operator. However, this restriction also blocks access to legitimate static functions in the prototype that should be allowed to execute without creating an object:
function TestIt() {
if(this.constructor == arguments.callee && !this._constructed )
this._constructed = true;
else
throw "this function must be called with the new operator";
}
TestIt.prototype.someStaticMethod=function() {
console.log('hello');
}
TestIt.prototype.someStaticMethod(); //acceptable
var t=new TestIt();
t.someStaticMethod(); //acceptable
TestIt.someStaticMethod(); //exception thrown
Is there any workaround to make TestIt.someStaticMethod()
function properly in this scenario?
Why does calling TestIt.someStaticMethod()
actually summon the constructor? It seems counterintuitive for it to behave this way.