Created
March 4, 2015 22:29
-
-
Save alexreardon/959ee3281938afc06323 to your computer and use it in GitHub Desktop.
Simple JavaScript inheritence
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
(function() { | |
var person = { | |
sayName: function() { | |
console.log('my name is ' + this.name); | |
} | |
}; | |
var bob = Object.create(person); | |
bob.name = 'bob'; | |
var teacher = Object.create(person); | |
teacher.saySomething = function() { | |
this.sayName(); | |
console.log('who are you?'); | |
}; | |
var sam = Object.create(teacher); | |
sam.name = 'sam'; | |
sam.saySomething(); | |
bob.sayName(); | |
console.log(teacher.isPrototypeOf(sam)); | |
console.log(Object.getPrototypeOf(sam) === teacher); | |
console.log(person.isPrototypeOf(teacher)); | |
// multi level | |
// person -> teacher -> sam | |
console.log(person.isPrototypeOf(sam)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No need to use
new
which is really misleading. No direct modification ofprototype
or__proto__
. Simple, clean inheritance as it was intended! OLOO (Objects Linked to Other Objects)