Skip to content

Instantly share code, notes, and snippets.

@shinyzhu
Created February 10, 2012 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinyzhu/1787245 to your computer and use it in GitHub Desktop.
Save shinyzhu/1787245 to your computer and use it in GitHub Desktop.
Inheritance in javascript
// Practices from Object Oriented Javascript
// version 0.0.0.11
(function() {
function multi() {
var n = {},
stuff, j = 0,
len = arguments.length;
for (j = 0; j < len; j++) {
stuff = arguments[j];
for (var i in stuff) {
n[i] = stuff[i];
}
}
return n;
}
var shape = {
name: 'Shape',
toString: function() {
return this.name;
}
};
var twoDee = {
name: '2D Shape',
dimensions: 2
};
var triangle = multi(shape, twoDee, {
name: 'Triangle',
getArea: function() {
return this.side * this.height / 2;
},
side: 5,
height: 10
});
//test
console.info(triangle.getArea()); //->25
console.info(triangle.dimensions); //->2
console.info(triangle.toString()); //->Triangle
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment