Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created September 10, 2017 03:42
Show Gist options
  • Save StevenJL/09d45278bce69d7742bb75c9874c4229 to your computer and use it in GitHub Desktop.
Save StevenJL/09d45278bce69d7742bb75c9874c4229 to your computer and use it in GitHub Desktop.
classes
// Before ES6
function Person(name, age) {
this.name = name;
this.age = age;
}
// a greeter method on a Person object
Person.prototype.greet = function() {
console.log('Hi, I am ' + this.name);
}
// a "class method" on the Person "class"
Person.older = function(person1, person2) {
return (person1.age >= person2.age) ? person1 : person2
}
// With ES6
class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log('Hi, I am ' + this.name);
}
/*
note the static keyword makes the method into
a class method, so it's meant to be invoked on
the class, not an instance of the class.
*/
static order (person1, person2) {
return (person1.age >= person2.age) ? person1 : person2
}
}
// class inheritance
class AngryPerson extends Person {
constructor (name, age, favorite_expletive) {
super(name, age);
this.favorite_expletive = favorite_expletive;
}
yell() {
console.log(this.favorite_expletive + '!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment