Is it possible to set something of a default/fallback function for a JavaScript class?
var a = new function() {
this.b = function() {
return "B";
}
return "A";
}
In this example, I’d want to see B when executing alert(a.b()), and A for alert(a) alone.
If you overwrite the
toStringmethod of an object you can get it to return whatever you want.Although you may be trying to return literal values from a constructor. You can only return objects from a constructor.
You may also want to overwrite
.valueOf