I have a function for creating objects as follows:
function person() {
this.name = "test person";
}
var me = new person();
Now I'm planning to wrap this function into another one like this for assertion purposes.
function reliable_person() {
/* do check on params if any*/
return new person();
}
var me = new reliable_person();
Is this a possible approach? I know there are better ways for such checks within the constructor itself, but is this a reliable solution?
newinnew reliable_personis entirely superfluous.reliable_personis just a factory function, not an object constructor.reliable_personfunction behaves like a factory pattern so no call withnewkeyword is required when an object is created throughreliable_person()function.factory function. I think this would solve your problem.