Created
November 17, 2011 23:09
-
-
Save mzgoddard/1374868 to your computer and use it in GitHub Desktop.
Prototypical Inheritance with Object.create
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 type(prototype, members, properties, objectMembers) { | |
var exports = {}, key; | |
properties = properties || {}; | |
objectMembers = objectMembers || {}; | |
exports.prototype = Object.create(prototype || {}, properties); | |
for ( key in members ) { | |
exports.prototype[key] = members[key]; | |
} | |
if ( !exports.prototype.init ) { | |
exports.prototype.init = function() {}; | |
} | |
for ( key in objectMembers ) { | |
exports[key] = objectMembers[key]; | |
} | |
if ( !exports.create ) { | |
exports.create = function() { | |
var o = Object.create(exports.prototype); | |
o.init.apply(o, arguments); | |
return o; | |
}; | |
} | |
return exports; | |
} |
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
var Bounds = type( | |
// empty prototype, no parent | |
{}, | |
{ | |
init: function( x, y, w, h ) { | |
this.x = x; | |
this.y = y; | |
this.width = w; | |
this.height = h; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment