Created
January 19, 2015 23:55
-
-
Save tagr/c974bae1bb0f82432ab1 to your computer and use it in GitHub Desktop.
An "implements" function inspired by interfaces in C#, for JavaScript. Interface properties are copied into the receiving object's prototype collection, which become available as instance properties.
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
/** | |
* Extends the Object prototype to provide basic support for interface-like structures and declarations. | |
* @param {Object[]} - comma-delimited list of objects with properties to implement. | |
* @example | |
* // returns Class object with IClass' someFunction property. Instances of Class will have this property defined. | |
* var IClass = {someFunction:function(){}}; | |
* var Class = function(){}.implements(IClass); | |
* @returns {Object} | |
*/ | |
Object.prototype.implements = function() { | |
var i = arguments.length; | |
while (i--) { | |
var interface = arguments[i]; | |
for(var property in interface) { | |
if (!!this.prototype && !this.prototype.hasOwnProperty(property)) { | |
this.prototype[property] = interface[property]; | |
} | |
} | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment