Created
February 17, 2023 10:41
-
-
Save prashantsani/1757a36190c968216faa37feffc28db5 to your computer and use it in GitHub Desktop.
Classical Inheritance
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
| function Car(options) { | |
| this.title = options.title; | |
| } | |
| Car.prototype.drive = function () { | |
| return 'drive' | |
| } | |
| function Toyota(options) { | |
| Car.call(this, options); | |
| this.color = options.color; | |
| } | |
| Toyota.prototype = Object.create(Car.prototype); | |
| Toyota.constructor = Toyota; | |
| Toyota.prototype.honk = function () { | |
| return 'honk' | |
| } | |
| const toyota = new Toyota( { color: 'red', title: 'new toyota' }); | |
| console.log(toyota) | |
| console.log(toyota.drive()) | |
| console.log(toyota.honk()) | |
| // results:- | |
| // {"title":"new toyota","color":"red"} | |
| // drive | |
| // honk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment