Improving Function.prototype.bind

François REMY fremycompany_pub at yahoo.fr
Fri Jan 6 01:30:37 PST 2012


My proposal could break existing code so it’s probably not worth a shot in ES6. Maybe in ES7, then.

From: Brendan Eich 
Sent: Friday, January 06, 2012 12:52 AM
To: François REMY 
Cc: Axel Rauschmayer ; Andrea Giammarchi ; es-discuss 
Subject: Re: Improving Function.prototype.bind

What *exactly* would you do here? Making methods auto-bind on extraction, with memoization? Please be concrete and specific.


It would be to do the same thing as the old IE behavior with host functions.

var $ = document.getElementById;
$(‘id’) // works as expected, on document
$.call(anotherDocument, ‘id’); // works as expected, on anotherDocument
setImmediate(myObj.asyncAction); // works as expected

That means that a function reference returned by obj.func would have a “default” this to be used when there’s no this available.
Implementation may use an internal [[boundFunctions]] property to make sure to return the same “boundFunction” instance each time when accessed on an object. Or another trick could be used as operator overloading to make it feel like that while it’s not true (but it would need to update native functions like addEventListener as well, which is not easy/efficient).

Please note that the current behavior of using a “Reference” doesn’t need to be updated, it’s just that when the reference is flattened to a “Function” it should be flattened in a “Default-Bound Function” instead. For most of the code I see on the web, it would not make any difference.

This would not change the behavior of the bind function which makes a function whose “this” is ALWAYS the boundObject (consider document.querySelector.bind(document).call(document.body.firstChild, "body"))

François
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120106/85248427/attachment.html>


More information about the es-discuss mailing list