The prototype is the place for shared properties.
Consider:
var protoCircle - {x: 0, y: 0, radius: 1}
This means every circle will have these properties as defaults. So if I make circle with that prototype:
var c1 = Object.create(protoCircle);
c1.x = 3;
var c2 = Object.create(protoCircle);
c2.y = 5;
then circle c1
is at (3,0) with radius 1 and c2
is at (0,5) with radius 1. That's because c1
has only one own property (x
) and two inherited properties (y
and radius
) which it picks up from its prototype. If I change protoCircle.radius
then that change will be seen by the two circles. That is the way JavaScript is designed! This design allows a whole bunch of objects to share a default value so you don't have to store it in each object. You just put the properties unique to each object inside each object and let the default properties be shared in the prototype. In the case above, if 99% of the circles have a radius of 1, then we don't have to store radii in our individual circles.
In your case you put a property called element
in a prototype. All objects that share that prototype will have the same value for element
. So if you say
x.element.className = 'something'
then that does effectively make y.element.className
be something
as well, assuming y
shares the same prototype as x
.
If you want each of the named rounded blocks to have different elements then you need to do this:
function NamedRoundedBlock(){
...
this.element = {}
this.element.className = "Block NamedRound root";
this.element.appendChild(name);
}