Created
October 20, 2018 11:09
-
-
Save oyilmaztekin/e2d32997acfa3812d8340ffcd41e23c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment