Created
December 19, 2015 09:43
-
-
Save nvurgaft/050ac536f1e6632fda94 to your computer and use it in GitHub Desktop.
Class declartion and inheritance in Javascript ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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