Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 402 Bytes

File metadata and controls

24 lines (19 loc) · 402 Bytes

手写 bind

题目描述

写一个函数,实现Function.prototype.bind的功能。

代码

Function.prototype.myBind = function(ctx, ...args) {
  return (...innerArgs) => this.call(ctx, ...args, ...innerArgs);
};

// test
const a = {
  name: "name of a"
};
function test(...msg) {
  console.log(this.name);
  console.log(...msg);
}
const t = test.myBind(a, "hello");
t("world");