Skip to content

Instantly share code, notes, and snippets.

@mzgoddard
Created November 17, 2011 23:09
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 mzgoddard/1374868 to your computer and use it in GitHub Desktop.
Save mzgoddard/1374868 to your computer and use it in GitHub Desktop.
Prototypical Inheritance with Object.create
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;
}
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