Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Created November 16, 2016 03:54
Show Gist options
  • Save dengjonathan/a7e932296571317f796842e4c7e4684b to your computer and use it in GitHub Desktop.
Save dengjonathan/a7e932296571317f796842e4c7e4684b to your computer and use it in GitHub Desktop.
oop inheritance
//OOP
class Parent {
constructor(name, job) {
this.name = name;
this.job = job;
}
}
const solo = new Parent('Han', 'smuggler'); // {name: 'Han', job: 'smuggler'}
class Child extends Parent {
constructor(name, job, hero) {
this.name = name;
this.job = job;
this.hero = hero;
}
}
const ren = new Child('Kylo', 'Knight of Ren', 'Darth Vader')
// {name: 'Kylo', job: 'Knight of Ren', hero: 'Darth Vader'}
@Gesparo
Copy link

Gesparo commented Nov 16, 2016

I think you can call super() method to init name and job
Example:

class Child extends Parent {
constructor(name, job, hero) {
super(name, job);
this.hero = hero;
}
}

Documentation:
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Classes/extends
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment