Created
October 31, 2019 18:59
-
-
Save bapinmalakar/b7778a78ae7b1d13ab38462af457690f to your computer and use it in GitHub Desktop.
This file contains 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
class Parent { | |
constructor(father_name, mother_name, city_name) { | |
this.father_name = father_name; | |
this.mother_name = mother_name; | |
this.city_name = city_name; | |
} | |
printParentDetails(){ | |
console.log('Father Name: ', this.father_name); | |
console.log('Mother Name: ', this.mother_name); | |
console.log('They live in: ', this.city_name) | |
} | |
} | |
class Country { | |
constructor(country_name, country_code) { | |
this.country_name = country_name; | |
this.country_code = country_code; | |
} | |
printCountryDetails() { | |
console.log('Country Name: ', this.country_name); | |
console.log('Country Code: ', this.country_code); | |
} | |
} | |
class Child { | |
constructor(father_name, mother_name,city_name, name, age, country_name, country_code) { | |
extendClass(this, new Parent(father_name, mother_name,city_name)); | |
extendClass(this, new Country(country_name, country_code)); | |
this.name = name; | |
this.age = age; | |
} | |
printChildDetails(){ | |
this.printParentDetails(); | |
console.log('Child name is: ', this.name); | |
console.log('Child age is: ', this.age) | |
} | |
} | |
function extendClass(child, parent) { | |
for(let key in parent){ | |
child[key] = parent[key] | |
} | |
Reflect.ownKeys(Reflect.getPrototypeOf(parent)).filter(d=> d!= 'constructor').map(fun=>{if(!child[fun]) {child[fun] = parent.__proto__[fun].bind(child);}}); | |
} | |
const child_1 = new Child('Jonny', 'Jolly', 'New York', 'Jin', 18); | |
const child_2 = new Child('Ram', 'Sham', 'Mumbai', 'Jadu', 21); | |
child_1.printChildDetails(); | |
child_2.printChildDetails(); |
This file contains 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
const Parent = function(father_name, mother_name, city_name) { | |
this.father_name = father_name; | |
this.mother_name = mother_name; | |
this.city_name = city_name; | |
this.printParentDetails= ()=>{ | |
console.log('Father Name: ', this.father_name); | |
console.log('Mother Name: ', this.mother_name); | |
console.log('They live in: ', this.city_name); | |
} | |
} | |
const Country = function(country_name, country_code) { | |
this.country_name = country_name; | |
this.country_code = country_code; | |
this.printCountryDetails= ()=> { | |
console.log('Country Name: ', this.country_name); | |
console.log('Country Code: ', this.country_code); | |
} | |
} | |
const Child = function(father_name, mother_name,city_name, name, age, country_name, country_code) { | |
Parent.call(this,father_name, mother_name, city_name); | |
Country.call(this, country_name,country_code); | |
this.name = name; | |
this.age = age; | |
this.printChildDetails = ()=>{ | |
this.printParentDetails(); | |
this.printCountryDetails(); | |
console.log('Child name is: ', this.name); | |
console.log('Child age is: ', this.age); | |
} | |
} | |
Child.prototype = Object.create(Parent.prototype); | |
Child.prototype = Object.create(Country.prototype); | |
Child.prototype.constructor = Child; | |
const child_1 = new Child('Jonny', 'Jolly', 'Washington', 'Jin', 18, 'US', '+1'); | |
const child_2 = new Child('Ram', 'Sham', 'Mumbai', 'Jadu', 21, 'India', '+91'); | |
child_1.printChildDetails(); | |
child_2.printChildDetails(); |
This file contains 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
class Parent { | |
constructor(father_name, mother_name, city_name) { | |
this.father_name = father_name; | |
this.mother_name = mother_name; | |
this.city_name = city_name; | |
} | |
printParentDetails(){ | |
console.log('Father Name: ', this.father_name); | |
console.log('Mother Name: ', this.mother_name); | |
console.log('They live in: ', this.city_name); | |
} | |
} | |
class Child extends Parent{ | |
constructor(father_name, mother_name,city_name, name, age) { | |
super(father_name, mother_name, city_name); | |
this.name = name; | |
this.age = age; | |
} | |
printChildDetails(){ | |
this.printParentDetails(); | |
console.log('Child name is: ', this.name); | |
console.log('Child age is: ', this.age); | |
} | |
} | |
const child_1 = new Child('Jonny', 'Jolly', 'New York', 'Jin', 18); | |
const child_2 = new Child('Ram', 'Sham', 'Mumbai', 'Jadu', 21); | |
child_1.printChildDetails(); | |
child_2.printChildDetails(); |
This file contains 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
const Parent = function(father_name, mother_name, city_name) { | |
this.father_name = father_name; | |
this.mother_name = mother_name; | |
this.city_name = city_name; | |
this.printParentDetails= ()=>{ | |
console.log('Father Name: ', this.father_name); | |
console.log('Mother Name: ', this.mother_name); | |
console.log('They live in: ', this.city_name); | |
} | |
} | |
const Child = function(father_name, mother_name,city_name, name, age) { | |
Parent.call(this,father_name, mother_name, city_name); | |
this.name = name; | |
this.age = age; | |
this.printChildDetails = ()=>{ | |
this.printParentDetails(); | |
console.log('Child name is: ', this.name); | |
console.log('Child age is: ', this.age); | |
} | |
} | |
Child.prototype = Object.create(Parent.prototype); | |
Child.prototype.constructor = Child; | |
const child_1 = new Child('Jonny', 'Jolly', 'New York', 'Jin', 18); | |
const child_2 = new Child('Ram', 'Sham', 'Mumbai', 'Jadu', 21); | |
child_1.printChildDetails(); | |
child_2.printChildDetails(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Single and multiple inheritance in JavaScript using both prototype and class