The values "x, y, width and height" are inherited by the object created by the "SquareFactory" from the object created by the ShapeFactory, but the function right() is not inherited. I assume the line
square.protoype = ShapeFactory();
creates the link for the properties to be searched for. But this does not seem to happen. Am i missing something about the prototype. I read How does JavaScript .prototype work? but not able to find the error with my current understanding about prototype.
function ShapeFactory() {
return {
x: 0,
y: 0,
width: 0,
height: 0,
right: function () {
return this.x + this.width;
}
};
}
function SquareFactory() {
var square = {
insideBoundary: function (x, y) { // this function works
return (x > this.x && x < (this.x + this.width)
&& y > this.y && y < (this.y + this.height));
},
print_info: function () { // this function throws error
console.log(this.x + " " + this.y + " "
+ this.width + " " + this.height
+ this.right() + " " + this.bottom()); // error accessing right() fn
}
};
square.protoype = ShapeFactory();
return square;
}