Created
February 10, 2012 06:45
-
-
Save shinyzhu/1787245 to your computer and use it in GitHub Desktop.
Inheritance in javascript
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
// 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