JavaScript如何创build可调用的实例

我想创build扩展原型函数的实例,以避免monkey-patching Function.prototype并避免复制属性到每个函数实例。

通过inheritance函数原型似乎不可能。

 var util = require('util'); function Fn () {} util.inherits(Fn, Function); var fn = new Fn('a', 'return a;'); fn.call(null, 5); // TypeError: object is not a function fn.apply(null, [ 5 ]); // TypeError: Function.prototype.toString is not generic // at Fn.toString (native) 

编辑 :我为什么要这样做?

试图编写可链接的高阶函数来处理promise。

 when(' promise').then(library.trim().toUpperCase()) // becomes 'PROMISE'