-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
Function::bind when it is available. related: #1363
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1769,7 +1769,17 @@ UTILITIES = | |
|
||
# Create a function bound to the current value of "this". | ||
bind: ''' | ||
function(fn, me){ return function(){ return fn.apply(me, arguments); }; } | ||
(Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
michaelficarra
Author
Collaborator
|
||
return function bound(){ | ||
if (!(this instanceof bound)) | ||
return fn.apply(me, arguments); | ||
var ctor = function(){}; | ||
ctor.prototype = fn.prototype; | ||
var obj = new ctor(); | ||
fn.apply(obj, arguments); | ||
return obj; | ||
}; | ||
}) | ||
''' | ||
|
||
# Discover if an item is in an array. | ||
|
6 comments
on commit 8d6e33c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crap, commit message is supposed to say "fixed context"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh .. what's up with the utility function? Can we leave this off master until we have an uncontroversial implementation of bind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That test case was broken. new => this
should produce the instance of the anonymous method, not the outer context. Now Function::bind
and our own __bind
behave the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new =>
has use cases that deserve 7 lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All three commits reverted on master -- if we want to explore alternative bind
functions, we can do it on a branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jashkenas: so what do you suggest we do about the errant test case and broken bind function? You said we should use the native bind when it's available (which is pretty much always), but we can't do that if it has different behaviour than the one we provide. It wasn't that big of an addition.
Function.bind ? Function.call.bind(Function.bind) : function(fn, me) {...}