I am trying to learn how to work with ‘classes’ in javascript.
Here is my code:
function Shape(x, y) {
this.x= x;
this.y= y;
}
Shape.prototype.toString= function() {
return 'Shape at '+this.x+', '+this.y;
};
function Circle(x, y, r) {
Shape.call(this, x, y); // invoke the base class's constructor function to take co-ords
this.r= r;
}
Circle.prototype= $.extend(true, {}, Shape.prototype);
Circle.prototype.toString= function() {
return 'Circular '+Shape.prototype.toString.call(this)+' with radius '+this.r;
}
var c = new Circle(1,2,3);
alert(c);
Is there a way to define the Shape’s toString function inside it’s constructor, or it does not make sense in this situation?
Base on my understanding:
Example: http://jsfiddle.net/paptamas/qDSkj/
Example: http://jsfiddle.net/paptamas/cbnLB/
In other words, explicit members have priority over prototype definitions and when you say
you are defining that function as a member of your instance (as oppose to member of your type – which is in a way not optimized as well).
Regards.