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.
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