Skip to content

Instantly share code, notes, and snippets.

@alexreardon
Created March 4, 2015 22:29
Show Gist options
  • Save alexreardon/959ee3281938afc06323 to your computer and use it in GitHub Desktop.
Save alexreardon/959ee3281938afc06323 to your computer and use it in GitHub Desktop.
Simple JavaScript inheritence
(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));
})();
@alexreardon
Copy link
Author

No need to use new which is really misleading. No direct modification of prototype or __proto__. Simple, clean inheritance as it was intended! OLOO (Objects Linked to Other Objects)

@mrowles
Copy link

mrowles commented Mar 4, 2015

Engarde!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment