I'm not sure if you're aware that your object will only be an instance of CallablePoint
if you use the new
keyword. By naming it "callable", you make me think you don't want to use new
. Anyway, there is a way to force an instance to be returned (thanks for the tip, Resig):
function CallablePoint(x, y) {
if (this instanceof CallablePoint) {
// Your "constructor" code goes here.
// And don't return from here.
} else {
return new CallablePoint(x, y);
}
}
This will return an instance of CallablePoint
regardless of how it was called:
var obj1 = CallablePoint(1,2);
console.log(obj1 instanceof CallablePoint); // true
var obj2 = new CallablePoint(1,2);
console.log(obj2 instanceof CallablePoint); // true