Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.