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

Add Function.bind #318

Closed
gfwilliams opened this issue Apr 9, 2014 · 6 comments
Closed

Add Function.bind #318

gfwilliams opened this issue Apr 9, 2014 · 6 comments

Comments

@gfwilliams
Copy link
Member

Seems to be required by some people...

http://es5.github.io/#x15.3.4.5

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

@gfwilliams
Copy link
Member Author

Simple workaround (without args):

Function.prototype.bind = function(thisArg) {
  var f = this;
  return function() { return f.apply(thisArg, arguments); };
};

@rwaldron
Copy link
Contributor

rwaldron commented Apr 9, 2014

Alternative, with args support:

Function.prototype.bind = function(thisArg) {
  var f = this;
  var bArgs = [].slice.call(arguments, 1);
  return function() { 
    return f.apply(thisArg, [].concat.apply(bArgs, arguments)); 
  };
};

function f(a, b, c) {
  console.log(this, a, b, c);
}

var bound = f.bind({}, 1, 2);

bound(3);

// {} 1 2 3

@gfwilliams
Copy link
Member Author

This could be really good for outputting data quickly. For example:

var pins = [A7,A6,A5,A4,A3,A2,A1,A0];
myData.forEach(digitalWrite.bind(undefined, pins));

Also for passing data into code created with inline assembler...

So it definitely needs to work for built-in functions too - and efficiently.

@rwaldron
Copy link
Contributor

As long as it's defined on Function.prototype then it will work on all built-ins, right?

@gfwilliams
Copy link
Member Author

Yes - but it's more how we do the implementation if we want it to be faster than if we'd just used the JS replacement above.

jwsCallFunction does 2 completely different things depending on whether the function is native or not - not to mention whether it is parsing or not :(

@rwaldron
Copy link
Contributor

Indeed, bind in plain js is slow

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

No branches or pull requests

2 participants