【js-bind】手写bind
<h4>手写bind</h4>
<hr />
<h4>题目描述</h4>
<p>写一个函数,实现Function.prototype.bind的功能。</p>
<h3>代码</h3>
<pre><code>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");</code></pre>