this series as effective JavaScript 's reading notes.
Suppose you need to Item in the Actor object to join a ID Information:
function Actor (scene, X, y) {this.scene = Scene;this.x = X;this.y = Y;this.id = ++actor.nextid;scene.register (this);} Actor.nextid = 0;
at the same time. You also need to include the ID information in the Actor 's subtype Alien :
function Alien (scene, X, y, direction, speed, strength) {Actor.call (this, scene, X, y); this.direction = Direction;this.spe ed = Speed;this.strength = Strength;this.damage = 0;this.id = ++alien.nextid; Conflicts with actor id!} Alien.nextid = 0;
in alien Also in the constructor of id property was assigned. So. In alien id property is always passed alien.nextid actor.nextid .
The ID property of the parent type overrides the ID property of the quilt type .
The workaround is also very easy, using different property names in different types:
function Actor (scene, X, y) {this.scene = Scene;this.x = X;this.y = Y;this.actorid = ++actor.nextid;//distinct from Alie Nidscene.register (this);} Actor.nextid = 0;function Alien (scene, X, y, direction, speed, strength) {Actor.call (this, scene, X, y); this.direction = d Irection;this.speed = Speed;this.strength = Strength;this.damage = 0;this.alienid = ++alien.nextid; Distinct from actorid}alien.nextid = 0;
Summarize
- Note the attribute names that are used in all parent types do not and are repeated in subtypes.
- Do not reuse property names that are already used in the parent type in subtypes.
Effective JavaScript Item 39 never reuse property names in a parent type