Last active
September 12, 2022 21:38
-
-
Save finalfantasia/0d5550b27cb049ad606f to your computer and use it in GitHub Desktop.
Pseudo-classical Inheritance in JavaScript
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
// Phone | |
var Phone = function (phoneNumber) { | |
this._phoneNumber = phoneNumber; | |
}; | |
Phone.prototype.getPhoneNumber = function () { | |
return this._phoneNumber; | |
}; | |
Phone.prototype.getDescription = function () { | |
console.log ('The simplest thing that works.'); | |
}; | |
// SmartPhone | |
var SmartPhone = function (phoneNumber, signature) { | |
Phone.call (this, phoneNumber); | |
this._signature = (signature || 'sent from ' + this.getDescription ()); | |
}; | |
// Inheritance | |
SmartPhone.prototype = Object.create (Phone.prototype); | |
SmartPhone.prototype.sendEmail = function (emailAddress, message) { | |
console.log ('Sending email to: ' + emailAddress + '. Message: ' + message + '\n' + this._signature); | |
}; | |
var phone = new Phone ('8675309'), | |
smartPhone = new SmartPhone ('5555555', 'Adios'); | |
smartPhone.sendEmail ('[email protected]', 'I can send email from my phone!'); | |
console.log ('SmartPhone is ' + (smartPhone instanceof Phone ? '' : ' not') + 'a Phone'); | |
console.log ('Phone is' + (phone instanceof SmartPhone ? '' : ' not') + ' a SmartPhone'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment