Effective JavaScript Item 39 never reuse property names in a parent type

Source: Internet
Author: User

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

    1. Note the attribute names that are used in all parent types do not and are repeated in subtypes.
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.