【发布时间】:2014-11-10 00:38:46
【问题描述】:
这是我的 compose 函数,作为一个 polyfill
Function.prototype.compose = function(prevFunc) {
var nextFunc = this;
return function() {
return nextFunc.call(this, prevFunc.apply(this,arguments));
}
}
这些工作:
function function1(a){return a + ' do function1 ';}
function function2(b){return b + ' do function2 ';}
function function3(c){return c + ' do function3 ';}
var myFunction = alert(function1).compose(function2).compose(function3);
myFunction('do');
var roundedSqrt = Math.round.compose(Math.sqrt)
roundedSqrt(6);
var squaredDate = alert.compose(roundedSqrt).compose(Date.parse)
quaredDate("January 1, 2014");
但这不起作用!
var d = new Date();
var alertMonth = alert.compose(getMonth); <--
alertMonth(d); ^^^^
错误在谷歌浏览器中引发错误“未捕获的 ReferenceError:getMonth 未定义”。
现在,如果我尝试以下任何一种方法:
var d = new Date();
function pluckMonth(dateObject) {return dateObject.getMonth();}
var alertMonth = alert.compose(pluckMonth);
var alertMonth2 = alert.compose(function(d){return d.getMonth()});
alertMonth(d);
alertMonth2(d);
他们工作。
好的,那么,这是为什么呢?我不想编写额外的函数,我希望它能够正常工作。 compose 函数使用apply 实用程序,而this 仅用于thisArg,因此它应该适用于对象成员以及独立函数,对吧??
也就是说,它们是等价的
this.method()
method.call.apply(this)
jsFiddle: http://jsfiddle.net/kohq7zub/3/
【问题讨论】:
-
getMonth不是函数,它是Date.prototype的属性。 -
试试
compose(Date.prototype.getMonth)。 -
我有,我已经尝试了所有这些小技巧。如果您想自己尝试,我提供了一个 jsfiddle:jsfiddle.net/kohq7zub/3
-
是的,我自己试过了,没用。问题是
this没有通过组合正确传播。我不确定您是否可以像这样等效地对待普通函数和方法。
标签: javascript methods function-composition modifier