Skip to content

Instantly share code, notes, and snippets.

@tagr
Created January 19, 2015 23:55
Show Gist options
  • Save tagr/c974bae1bb0f82432ab1 to your computer and use it in GitHub Desktop.
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.
/**
* 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