Created
March 12, 2015 16:11
-
-
Save furf/4c991bd1d4e314d71b37 to your computer and use it in GitHub Desktop.
Yet another prototypal inheritance scheme.
This file contains hidden or 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
/** | |
* Base inheritable/mixable object with `create` and `mixin` functionality. | |
* @type {Object} | |
*/ | |
var Base = { | |
/** | |
* Create a new instance of the object and mixin additional behaviors. | |
* @type {Object} Mixable behaviors. (optional, multiple) | |
* @return {Object} Instantiated child object. | |
*/ | |
create: function() { | |
var child = Object.create(this); | |
var i = 0; | |
var n = arguments.length; | |
for (; i < n; ++i) { | |
child.mixin(arguments[i]); | |
} | |
return child; | |
}, | |
/** | |
* Create a new instance of the object and mixin additional behaviors. | |
* @type {Object} Mixable behaviors. | |
* @return {Object} self | |
*/ | |
mixin: function(mixin) { | |
var key; | |
for (key in mixin) { | |
if (mixin.hasOwnProperty(key)) { | |
this[key] = mixin[key]; | |
} | |
} | |
return this; | |
}, | |
/** | |
* [isInstanceOf description] | |
* @param {[type]} prototype [description] | |
* @return {Boolean} [description] | |
*/ | |
isInstanceOf: function(prototype) { | |
return prototype.isPrototypeOf(this); | |
}, | |
/** | |
* [getParent description] | |
* @return {[type]} [description] | |
*/ | |
getParent: function() { | |
return Object.getPrototypeOf(this); | |
}, | |
/** | |
* [isChildOf description] | |
* @param {[type]} prototype [description] | |
* @return {Boolean} [description] | |
*/ | |
isChildOf: function(prototype) { | |
return this.getParent() === prototype; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment