Skip to content

Instantly share code, notes, and snippets.

@nvurgaft
Created December 19, 2015 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvurgaft/050ac536f1e6632fda94 to your computer and use it in GitHub Desktop.
Save nvurgaft/050ac536f1e6632fda94 to your computer and use it in GitHub Desktop.
Class declartion and inheritance in Javascript ES6
// a simple class declaration in es6
class Person {
constructor(fname, lname) {
this.fname = fname;
this.lname = lname;
}
toString() {
return (this.fname + " " + this.lname);
}
}
// this class extends the Person
class Employee extends Person {
constructor(fname, lname, position) {
super(fname, lname);
this.position = position;
}
toString() {
return (this.fname + " " + this.lname + " is a " + this.position);
}
}
// instantiate and run a class method
var person = new Person("John", "Smith");
console.log(person.toString());
// instantiate the child class
var employee = new Employee("John", "Matrix", "Commando");
console.log(employee.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment