this series as effective JavaScript 's reading notes.
If you need to Item in the Actor object to add 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, it is necessary to Actor Sub-type Alien added in ID Information:
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 theAlienconstructor, also for theIDThe property is assigned a value. Therefore, inAlieninstance of the type,IDproperties are always passed through theAlien.nextidto be assigned instead ofActor.nextid. Of the parent typeIDproperty of the quilt typeIDproperty overrides the.
The solution is also simple to use 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 property names that are used in all parent types do not and are duplicated 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