<script>
Function.prototype.bind = function () {
var fn = this,
arr = [].slice.call(arguments),
obj = arr.shift();
return function () {
return fn.apply(obj, arr.concat([].slice.call(arguments)))
}
}
const obj = {
add: function (a, b, c) {
console.log(a, b, c);
return this.a + this.b;
}
}
const obja = {
a: 2,
b: 777777
}
var bindfn = obj.add.bind(obja, 10, 20)
console.log(bindfn(2));
</script>
<script>
const obj = {
add: function (a, b, c) {
console.log(a, b, c);
return this.a + this.b;
}
}
const obja = {
a: 2,
b: 777777
}
var bindfn = obj.add.bind(obja, 10, 20)
console.log(bindfn(2));
</script>
js中bind的实现
最新推荐文章于 2022-08-02 17:26:36 发布