Skip to content

Instantly share code, notes, and snippets.

@oyilmaztekin
Created October 20, 2018 11:09
Show Gist options
  • Select an option

  • Save oyilmaztekin/e2d32997acfa3812d8340ffcd41e23c4 to your computer and use it in GitHub Desktop.

Select an option

Save oyilmaztekin/e2d32997acfa3812d8340ffcd41e23c4 to your computer and use it in GitHub Desktop.

Revisions

  1. oyilmaztekin created this gist Oct 20, 2018.
    42 changes: 42 additions & 0 deletions class-inharitace.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    class Rectangle {
    constructor(w, h) {
    this.w = w;
    this.h = h;
    }
    }

    Rectangle.prototype.area = function(){
    return this.w * this.h
    }

    // BAD WAY
    /*
    takes first parameter and fill its value in the second h parameter
    if Square takes second arguments, it will be overriden by the super(h,h) method.
    it is so dangerous
    */

    // class Square extends Rectangle{
    // constructor(h){
    // super(h,h)
    // }
    // }


    //CORRECT WAY

    class Square extends Rectangle {
    constructor(w,h) {
    super(w,h)
    if(!this.h) {
    this.h = this.w
    }
    }

    }

    const rec = new Rectangle(3, 4);
    const sqr = new Square(3);

    console.log(rec.area()); // it should return 12
    console.log(sqr.area()); // it should return 9