Skip to content

Instantly share code, notes, and snippets.

@kuanhsuh
Created April 22, 2018 02:32
Show Gist options
  • Save kuanhsuh/734f6dc98f71591ff858b57543b2d809 to your computer and use it in GitHub Desktop.
Save kuanhsuh/734f6dc98f71591ff858b57543b2d809 to your computer and use it in GitHub Desktop.
// es5 classes
// function Person(name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype.sayHi = function () {
// console.log(`Hello I am ${this.name}, and I am ${this.age} years old`)
// };
// var d = new Person('Danny', 30)
// d.sayHi()
// es6 classes
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHi() {
console.log(`Hello I am ${this.name}, and I am ${this.age} years old`)
}
}
// var e = new Person('Danny', 30)
// e.sayHi()
// Extends
// see often in react app
// class Boy extends Person {
// onlyBoy() {
// console.log(`${this.name} is a boy`)
// }
// }
// var f = new Boy('danny', '30')
// f.onlyBoy()
// Super if there's a constructor in subclass
// see very often in React
class Boy extends Person {
constructor(name, age, gender) {
super(name, age);
this.gender = gender
}
}
var g = new Boy('danny', 30, 'male')
console.log(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment