|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Function.prototype.bind behaviour (call vs apply)Hello everybody.
The currently proposed bind() method for function objects behaves like call(): it takes a number of arguments (from arguments[1] onwards), and uses them for the function execution. Wouldn't it be more flexible to behave like apply(), taking an array of parameters instead of taking each parameter individually ? The second approach seems more powerful to me. That is, every call() can easyly be replaced by apply(): func.call( thisObj, a, b, c ) -> func.apply( thisObj, [ a, b, c ] ) ...But when you only have got an array of arguments, you can't go the other way around: func.apply( thisObj, argsArray ) -> func.call( /* impossible */ ) Would it be possible to change bind to behave like apply ? Regards, Jordan OSETE _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
|
|
Re: Function.prototype.bind behaviour (call vs apply)On Aug 20, 2009, at 4:33 PM, Jordan Osete wrote: > Hello everybody. > > The currently proposed bind() method for function objects behaves > like call(): it takes a number of arguments (from arguments[1] > onwards), and uses them for the function execution. > > Wouldn't it be more flexible to behave like apply(), taking an array > of parameters instead of taking each parameter individually ? > > The second approach seems more powerful to me. That is, every call() > can easyly be replaced by apply(): > func.call( thisObj, a, b, c ) -> func.apply( thisObj, [ a, b, c ] ) > > ...But when you only have got an array of arguments, you can't go > the other way around: > func.apply( thisObj, argsArray ) -> func.call( /* impossible */ ) > > Would it be possible to change bind to behave like apply ? If `boundArgs` is an array of arguments to bind, then I think you should be able to do this via something like: Function.prototype.bind.apply(targetFn, [thisArg].concat(boundArgs)); // or maybe: boundArgs.unshift(thisArg); Function.prototype.bind.apply(targetFn, boundArgs); Which is not very elegant of course (first version also takes a performance hit by creating unnecessary Array object). -- kangax _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
|
|
Re: Function.prototype.bind behaviour (call vs apply)On Aug 21, 2009, at 8:11 PM, Juriy Zaytsev wrote:
> If `boundArgs` is an array of arguments to bind, then I think you > should be able to do this via something like: > > Function.prototype.bind.apply(targetFn, [thisArg].concat(boundArgs)); > > // or maybe: > > boundArgs.unshift(thisArg); > Function.prototype.bind.apply(targetFn, boundArgs); > > Which is not very elegant of course (first version also takes a > performance hit by creating unnecessary Array object). But it gets the job done. I think it's better to leave ES5 Function.prototype.bind as specified, based on Prototype's bind (and others like it), which take positional arguments to partially apply. Then with spread ( http://wiki.ecmascript.org/doku.php? id=harmony:spread ) in Harmony, you can write foo.bind(thisArg, ...argsArray) instead of the above bind.apply mouthful. /be _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
|
|
Re: Function.prototype.bind behaviour (call vs apply)
Juriy Zaytsev wrote:
No offense meant, but I find this quite ugly and not much readable. ;-) Same thing, and it forces you to alter boundArgs. Brendan Eich wrote : But it gets the job done.Yes, that's what Allen Wirfs-Brock told me, though it wasn't forwarded to the list. I included my reply to his message as an attachment. I have to admit spread looks quite exciting to me, unfortunately it won't be widely implemented and usable before long. The mail I sent to Allen is included as an attachment. Regards, Jordan OSETE Hm, it's a shame that bind is already locked. About the name... The mnemonic I usually use to tell apply() and call() apart is the fact that apply starts with "A", just like Array, and call doesn't. It may sound foolish, but maybe other people (especially beginners) use something similar. If that is the case, then a synonym of bind starting with "A" could be a good choice. http://thesaurus.reference.com/browse/bind lists "adhere", "attach" and "affix" with approximately the same meaning (please correct me if I am wrong, as english is not my first language). I personally like "affix", because it feels like apply (five letters, and a "A" followed by twice the same consonant), but it is obviously quite subjective. Also "attach" is already used for events, so it could be confusing. Another possibility would be to use something like bindArray(). It is longer to type, but it is unambiguous. As for the use case, I don't have one ready right now, unfortunately. I thought about the bind behavior while reading http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ some time ago... Oh, and by the way, sorry for the double post to the list. It is the result of a misunderstanding between me and a list admin (the first mail had been mistaken for spam). Regards, Jordan OSETE Allen Wirfs-Brock a écrit : It's too late to change for ES5 plus the specified form is already widely used by several frameworks. However, I agree with your argument that the apply-like formulation is more flexible. Such a form could conceivably be added in the next edition beyond ES5. Can you come up with a good name for the apply-like form and do you have any use cases where the added flexibility is actually needed.-----Original Message----- From: es-discuss-bounces@... [...- bounces@...] On Behalf Of Jordan Osete Sent: Thursday, August 20, 2009 11:46 PM To: es-discuss@... Cc: brendan@... Subject: Function.prototype.bind behaviour (call vs apply) Hello everybody. The currently proposed bind() method for function objects behaves like call(): it takes a number of arguments (from arguments[1] onwards), and uses them for the function execution. Wouldn't it be more flexible to behave like apply(), taking an array of parameters instead of taking each parameter individually ? The second approach seems more powerful to me. That is, every call() can easyly be replaced by apply(): func.call( thisObj, a, b, c ) -> func.apply( thisObj, [ a, b, c ] ) ...But when you only have got an array of arguments, you can't go the other way around: func.apply( thisObj, argsArray ) -> func.call( /* impossible */ ) Would it be possible to change bind to behave like apply ? Regards, Jordan OSETE _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
| Free embeddable forum powered by Nabble | Forum Help |