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

Polyfill of 「bind」 #52

Closed
PolluxLee opened this issue Jun 14, 2018 · 0 comments
Closed

Polyfill of 「bind」 #52

PolluxLee opened this issue Jun 14, 2018 · 0 comments
Labels

Comments

@PolluxLee
Copy link
Owner

Polyfill

Function.prototype.bind2 = function (ctx) {
  var fn = this;
  var context = ctx;
  var args = Array.prototype.slice.call(arguments, 1);

  return function () {
    var bindArgs = Array.prototype.slice.call(arguments);
    return fn.apply(context, args.concat(bindArgs));
  }
}

ES6

Function.prototype.bind2 = function (ctx, ...args1) {
  var fn = this;
  var context = ctx;
  var a = args1;

  return function (...args2) {
    return fn.apply(context, args1.concat(args2));
  }
}

Test

var obj = {
  name: "April"
}

function getInfo(age, sex) {
  return {
    name: this.name,
    age,
    sex
  }
}

var a = getInfo.bind2(obj, 21, 'girl')();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant