Skip to content

Instantly share code, notes, and snippets.

@alyssaq
Last active February 13, 2024 07:58
Show Gist options
  • Save alyssaq/8450495 to your computer and use it in GitHub Desktop.
Save alyssaq/8450495 to your computer and use it in GitHub Desktop.
Javascript Inheritance
function Animal(name) {
  this.name = name;
};
Animal.prototype.move = function(meters) {
  console.log(this.name+" moved "+meters+"m.");
};

function Snake() {
  Animal.apply(this, Array.prototype.slice.call(arguments));
};
Snake.prototype = new Animal();
Snake.prototype.move = function() {
  console.log("Slithering...");
  Animal.prototype.move.call(this, 5);
};

var sam = new Snake("Sammy the Python");
sam.move();

Or use util.inherits() from Node.js

###Resources https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model
http://book.mixu.net/node/ch6.html

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