Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. function Interface() {}
  2.  
  3. /**
  4. * Implements an interface.
  5. * @param {Object} implementation The interface implementation.
  6. * @param {Array<Object>} interfaces The implemented interfaces.
  7. */
  8. Interface.implements = function(implementation, interfaces) {
  9. interfaces = Array.prototype.slice.call(arguments, 1);
  10. window.addEventListener('load', function() {
  11. for (var i = 0, interface; interface = interfaces[i]; i++) {
  12. var properties = Object.keys(interface.prototype);
  13.  
  14. for (var j = 0, property; property = properties[j]; j++) {
  15. var interfaceProperty = interface.prototype[property];
  16. var implementationProperty = implementation.prototype[property];
  17.  
  18. if (typeof interfaceProperty === 'function' &&
  19. (typeof implementationProperty !== 'function' ||
  20. implementationProperty.length !== interfaceProperty.length)) {
  21. throw new Error('Abstract method ' + property + ' is not overriden');
  22. } else {
  23. implementation.prototype[property] = interfaceProperty;
  24. }
  25. }
  26. }
  27. });
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement