-
-
Save deradam/1570738 to your computer and use it in GitHub Desktop.
JavaScript Object 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
/** | |
* Just a test for JavaScript object inheritance | |
* | |
* User: giemza | |
* Date: 06.01.12 | |
* Time: 10:38 | |
*/ | |
// Human constructor, type is 'Function' | |
var Human = function(firstname, lastname) { | |
// public fields, accessible from outside | |
this.firstname = firstname; | |
this.lastname = lastname; | |
// private field, only accessible from inside, e.g., getFullname function | |
var fullname = firstname + ' ' + lastname; | |
this.getFullname = function() { | |
return fullname; | |
} | |
}; | |
// Human public method (overrides toString from Object) | |
Human.prototype.toString = function() { | |
return this.firstname + ' ' + this.lastname; | |
}; | |
// new human instance, type is 'Object' | |
var adam = new Human('Adam', 'Riese'); | |
// another human instance | |
var jan = new Human('Jan', 'Padawan'); | |
console.log('firstname + lastname: ' + adam.firstname + ' ' + adam.lastname); | |
// prints: firstname + lastname: Adam Riese | |
console.log('adam.fullname: ' + adam.fullname); | |
// prints: adam.fullname: undefined | |
console.log('adam.getFullname(): ' + adam.getFullname()); | |
// prints: adam.getFullname(): Adam Riese | |
console.log('adam.toString(): ' + adam); | |
// prints: adam.toString(): Adam Riese | |
console.log('jan.toString(): ' + jan); | |
// prints: jan.toString(): Jan Padawan | |
// ### Inheritance ### | |
// Hacker constructor, type Function | |
var Hacker = function(firstname, lastname) { | |
Human.apply(this, [firstname, lastname]); | |
// calls the prototypes constructor (like super() in Java) | |
// Hacker.prototype.__proto__.constructor(firstname, lastname); | |
// Hacker.prototype.firstname = firstname; | |
// Hacker.prototype.lastname = lastname; | |
// public field | |
this.hacks = true; | |
}; | |
// setting the prototype of the Hacker's prototype to the prototype of the Human | |
//Hacker.prototype.__proto__ = Human.prototype; | |
Hacker.prototype = new Human(); | |
//Hacker.prototype.constructor = Hacker; | |
// Hacker public method (overrides and calls Human.toString()) | |
Hacker.prototype.toString = function() { | |
// return Hacker.prototype.__proto__.toString.call(this) + ' hacks: ' + this.hacks; | |
return Human.prototype.toString.call(this) + ' hacks: ' + this.hacks; | |
}; | |
// new Hacker instance, type Object | |
var stefan = new Hacker('Stefan', 'Nafets'); | |
console.log('stefan.toString(): ' + stefan); | |
// prints: stefan.toString(): Stefan Nafets hacks: true | |
console.log('stefan.hacks: ' + stefan.hacks); | |
// prints: stefan.hacks: true | |
console.log('stefan.getFullname(): ' + stefan.getFullname()); | |
// prints: Stefan Nafets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment