M
Michael Haufe (\TNO\)
I'm developing a significantly sized library that will potentially see
widespread use and abuse. To prevent some of the abuse, I'd like to
ask for some pointers, and some of your experience in developing
scalable robust code, especially in regards to error handling, and
enforcement of proper Object usage.
For the sake of an example, what is your opinion of the following
design pattern?
----------------------------------------------------------------
function Point(x, y, z){
if(!(this instanceof Point))
throw new Error("Point is a constructor")
if(isNaN(+x))
throw new Error("The x argument '"+ x + "' is not a number.");
if(isNaN(+y))
throw new Error("The y argument '"+ y + "' is not a number.");
if(isNaN(+z))
throw new Error("The z argument '"+ z + "' is not a number.");
this.x = parseInt(x,10);
this.y = parseInt(y,10);
this.z = parseInt(z,10);
}
Point.prototype.toString = function(){
if(!(this instanceof Point))
throw new Error("toString() called in an invalid context.")
if(isNaN(+this.x))
throw new Error("The x property '"+ this.x + "' is not a
number.");
if(isNaN(+this.y))
throw new Error("The y property '"+ this.y + "' is not a
number.");
if(isNaN(+this.z))
throw new Error("The z property '"+ this.z + "' is not a
number.");
return this.x + ", " + this.y + ", " + this.z;
}
var p = new Point(1,2,3)
p.toString() // 1, 2, 3
p.x = 4;
p.y = 5;
p.z = "a";
p.toString(); //throws "The z property 'a' is not a number."
var str = p.toString;
str(); // throws "toString called in an invalid context"
var p2 = Point(1,2,3); //throws "Point is a constructor"
----------------------------------------------------------------
widespread use and abuse. To prevent some of the abuse, I'd like to
ask for some pointers, and some of your experience in developing
scalable robust code, especially in regards to error handling, and
enforcement of proper Object usage.
For the sake of an example, what is your opinion of the following
design pattern?
----------------------------------------------------------------
function Point(x, y, z){
if(!(this instanceof Point))
throw new Error("Point is a constructor")
if(isNaN(+x))
throw new Error("The x argument '"+ x + "' is not a number.");
if(isNaN(+y))
throw new Error("The y argument '"+ y + "' is not a number.");
if(isNaN(+z))
throw new Error("The z argument '"+ z + "' is not a number.");
this.x = parseInt(x,10);
this.y = parseInt(y,10);
this.z = parseInt(z,10);
}
Point.prototype.toString = function(){
if(!(this instanceof Point))
throw new Error("toString() called in an invalid context.")
if(isNaN(+this.x))
throw new Error("The x property '"+ this.x + "' is not a
number.");
if(isNaN(+this.y))
throw new Error("The y property '"+ this.y + "' is not a
number.");
if(isNaN(+this.z))
throw new Error("The z property '"+ this.z + "' is not a
number.");
return this.x + ", " + this.y + ", " + this.z;
}
var p = new Point(1,2,3)
p.toString() // 1, 2, 3
p.x = 4;
p.y = 5;
p.z = "a";
p.toString(); //throws "The z property 'a' is not a number."
var str = p.toString;
str(); // throws "toString called in an invalid context"
var p2 = Point(1,2,3); //throws "Point is a constructor"
----------------------------------------------------------------