Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added native Function.prototype.bind rendition #11

Merged
merged 3 commits into from Mar 6, 2012

Conversation

gibletto
Copy link
Contributor

No description provided.

@gibletto
Copy link
Contributor Author

It seems native is slower than using apply but here for coverage anyway!

@david-mark
Copy link
Member

Need to call "apply" method of native bind function, else passing a single additional argument (array).

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

I appreciate your help. Thanks!

@gibletto
Copy link
Contributor Author

So something like this...
return function (){ fn.bind(context).apply(context,prependArgs.concat(Array.prototype.slice.call(arguments)));};

which prob does nothing more than run the rendition1.js type implementation. oh well!

@david-mark
Copy link
Member

I was thinking of:

return fn.bind.apply(context, prependArgs);

@gibletto
Copy link
Contributor Author

But bind doesn't execute does it?

@david-mark
Copy link
Member

On Thu, Feb 16, 2012 at 3:16 PM, Paul Connolly <
reply@reply.github.com

wrote:

But bind doesn't execute does it?

Not sure what you mean. The wrappers just return the bound function.

@gibletto
Copy link
Contributor Author

if(Function.prototype.bind){
bind = function(fn, context){
var prependArgs = Array.prototype.slice.call(arguments, 2);
return fn.bind.apply(context, prependArgs);
};
}
Gives an error. Maybe I'll give this one a rest

@david-mark
Copy link
Member

Sorry, I am swamped today. Certainly the example I gave you was wrong.
It's seting - this - to the "context" argument when calling bind.

On Thu, Feb 16, 2012 at 4:55 PM, Paul Connolly <
reply@reply.github.com

wrote:

if(Function.prototype.bind){
bind = function(fn, context){
var prependArgs = Array.prototype.slice.call(arguments, 2);
return fn.bind.apply(context, prependArgs);
};
}
Gives an error. Maybe I'll give this one a rest


Reply to this email directly or view it on GitHub:
#11 (comment)

@david-mark
Copy link
Member

I believe you had it before. The rub is in passing along the extra
arguments. I don't typically bother with those for such functions (i.e.
"bind*" functions just set the - this - object).

Sorry for any confusion.

On Thu, Feb 16, 2012 at 5:20 PM, David Mark dmark.cinsoft@gmail.com wrote:

Sorry, I am swamped today. Certainly the example I gave you was wrong.
It's seting - this - to the "context" argument when calling bind.

On Thu, Feb 16, 2012 at 4:55 PM, Paul Connolly <
reply@reply.github.com

wrote:

if(Function.prototype.bind){
bind = function(fn, context){
var prependArgs = Array.prototype.slice.call(arguments, 2);
return fn.bind.apply(context, prependArgs);
};
}
Gives an error. Maybe I'll give this one a rest


Reply to this email directly or view it on GitHub:
#11 (comment)

@david-mark
Copy link
Member

If you think about it, you can shorten the previous version by using
Function.prototype.bind. Just need to concatenate the "context" argument
and apply using the function as the - this - object. Should be faster than
the old version.

On Thu, Feb 16, 2012 at 5:24 PM, David Mark dmark.cinsoft@gmail.com wrote:

I believe you had it before. The rub is in passing along the extra
arguments. I don't typically bother with those for such functions (i.e.
"bind*" functions just set the - this - object).

Sorry for any confusion.

On Thu, Feb 16, 2012 at 5:20 PM, David Mark dmark.cinsoft@gmail.comwrote:

Sorry, I am swamped today. Certainly the example I gave you was wrong.
It's seting - this - to the "context" argument when calling bind.

On Thu, Feb 16, 2012 at 4:55 PM, Paul Connolly <
reply@reply.github.com

wrote:

if(Function.prototype.bind){
bind = function(fn, context){
var prependArgs = Array.prototype.slice.call(arguments,
2);
return fn.bind.apply(context, prependArgs);
};
}
Gives an error. Maybe I'll give this one a rest


Reply to this email directly or view it on GitHub:
#11 (comment)

@david-mark
Copy link
Member

Off the top of my head, this would be the shortest rendition:-

var myBind = function(fn, thisObject) {
return fn.bind.apply(fn, Array.prototype.slice.call(arguments, 1));
};

I'd expect it to be the fastest as well.

On Thu, Feb 16, 2012 at 5:28 PM, David Mark dmark.cinsoft@gmail.com wrote:

If you think about it, you can shorten the previous version by using
Function.prototype.bind. Just need to concatenate the "context" argument
and apply using the function as the - this - object. Should be faster than
the old version.

On Thu, Feb 16, 2012 at 5:24 PM, David Mark dmark.cinsoft@gmail.comwrote:

I believe you had it before. The rub is in passing along the extra
arguments. I don't typically bother with those for such functions (i.e.
"bind*" functions just set the - this - object).

Sorry for any confusion.

On Thu, Feb 16, 2012 at 5:20 PM, David Mark dmark.cinsoft@gmail.comwrote:

Sorry, I am swamped today. Certainly the example I gave you was wrong.
It's seting - this - to the "context" argument when calling bind.

On Thu, Feb 16, 2012 at 4:55 PM, Paul Connolly <
reply@reply.github.com

wrote:

if(Function.prototype.bind){
bind = function(fn, context){
var prependArgs = Array.prototype.slice.call(arguments,
2);
return fn.bind.apply(context, prependArgs);
};
}
Gives an error. Maybe I'll give this one a rest


Reply to this email directly or view it on GitHub:
#11 (comment)

@gibletto
Copy link
Contributor Author

That seems to have worked.

@david-mark
Copy link
Member

Should be faster than the - call - based wrapper as well. Add the feature
detection for Function.prototype.bind and that should do it.

Now we need a forked rendition, which will work in everything (save for IE
< 5.5 and the like), with better performance in newer browsers that feature
Function.prototype.bind.

Thanks!

On Thu, Feb 16, 2012 at 5:54 PM, Paul Connolly <
reply@reply.github.com

wrote:

That seems to have worked.


Reply to this email directly or view it on GitHub:
#11 (comment)

@gibletto
Copy link
Contributor Author

Ok forked rendition added as well as the update one.

david-mark pushed a commit that referenced this pull request Mar 6, 2012
Added native Function.prototype.bind rendition
@david-mark david-mark merged commit 6115d88 into david-mark-llc:master Mar 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants