radius is a property of each Circle object. It is initialised by the
radius parameter passed to the constructor function.
> Take the case of Circle looking like this:
>
> var Circle = Shape.extend({ // instance interface
> constructor: function (x, y, radius) {
> this.base(x, y);
> this.coords.push([x,y]);
> this.radius = radius;
> },
>
> radius: 0,
> coords: [],
>
> getCircumference: function () {
> return 2 * Circle.PI * this.radius;
> }
> }, { // class interface
> PI: 3.14
> });
>
> coords here is shared between all instances of Circles, and so the
> second circle created has two points in it's coords, the third three
> etc.
You can't define a coords object in the class definition. The coords
array would be shared by all instances. Instead, you should create a new
array in the constructor function:
var Circle = Shape.extend({ // instance interface
constructor: function (x, y, radius) {
this.coords = [x, y];
this.radius = radius;
},
radius: 0,
coords: null, // created in the constructor function
getCircumference: function () {
return 2 * Circle.PI * this.radius;
}
}, { // class interface
PI: 3.14
});
Is that a bit clearer?
If you want something simpler to study then you can look at John Resig's
Simple Inheritance:
http://ejohn.org/blog/simple-javascript-inheritance/
It is essentially a rewrite of Base but it is a bit easier to understand.
-dean