javascript - Why does Array.prototype.reduce not have a thisObject parameter? -
javascript array methods such foreach
have thisarg
parameter, used context invoking callback:
array.foreach(callback[, thisarg])
as every
, some
, filter
, map
. however, reduce
, reduceright
have no such parameter. there particular reason this, or reason not necessary?
for instance, consider next implementation of functional composition using reduceright
:
function compose () { var fns = [].slice.call(arguments,0); homecoming function result() { homecoming fns.reduceright( function (prev,cur){ homecoming [cur.apply(this,prev)]; }, arguments )[0]; }; }
i create "this-aware", functions beingness composed called in context in function returned compose
invoked. appear invoked in context of global object. old var self=this;
@ top of function result
, , utilize first argument cur.apply
call, have this
, unnecessary if reduce
took thisarg
argument.
am missing here, , there reduce
makes unnecessary or unuseful?
update
@kangax yes, occurred me. far me criticize design of api, signature reduce
seems bit unusual me. sec optional argument functions differently normal optional arguments, typically have default value; instead presence or absence changes behavior, overloading function based on signature (argument count). when sec parameter absent, first element of array becomes starting value , first phone call callback against sec value. seems me behavior emulated calling
array.slice(1).reduce(fn,array[0])
instead of building in special rules case sec argument omitted, in turn, if presumption correct, made impossible figure out specify thisarg
argument. again, sure such issues debated while spec beingness hashed out, , there may reasons such approach.
it becomes messy 2 optional arguments, since reduce
(right
) covers 2 functionalities (see wikipedia), distinguished in pure languages (e.g. named foldl
, foldl1
in haskell). cite brendan eich:
so mean cut down takes 1 callback argument , 2 optional arguments: thisobject , init. 1 should come first? more mutual 1 init, separate callback arg "thisobject" arg, maybe okay. multiple optional arguments kinda messy way...
alternatively, eliminate "thisobject" argument, since people can [use binding].
i don't think it's big issue, since these functional higher-order-functions used lamdba-function-expressions anyway (like in example). of course of study there's little inconsistency, can live that. image alternative:
array.reduce(callback[, initialvalue[, thisarg]])
can't used, cannot determine "if initialvalue provided" since means arguments.length < 2
- pass undefined
literally well. means
array.reduce(callback[, thisarg[, initialvalue]])
which ugly since needed pass null
or thisarg
if wanted initial value.
you noticed in comment kangax ("the sec optional argument functions differently normal optional arguments, […] presence or absence changes behavior"), can't back upwards statement
this behavior emulated calling array.slice(1).reduce(fn,array[0])
as a) not work complex (chained) look instead of array
variable , b) cumbersome.
javascript ecmascript-5
No comments:
Post a Comment