Coding / Programming Videos

Post your favorite coding videos and share them with others!

Minimalistic Interface Pattern for Javascript – Daelus Rascal – Medium

Source link

/**
* Simplified Interface Design Pattern for Javascript
*
* @interface
*
* @param {Object} Klass Prototype of JS class.
* @param {Array<function>} funcs Interface methods.
*/
module.exports = function InterfaceA(Klass, …funcs) {
let methods = { }
if (funcs.length > 0) {
for (let func of funcs) {
if (typeof func === ‘function’) {
methods[func.name] = func;
}
}
}
if (Klass) {
for (let method in methods) {
if (typeof methods[method] === ‘function’) {
Klass[method] = methods[method];
}
}
}
return Klass;
}

And here is how to use it:

InterfaceA(
Klass.prototype,
function methodA() {
// Do something here
}
);

Source link

Bookmark(0)
 

Leave a Reply

Please Login to comment
  Subscribe  
Notify of
Translate »