利用闭包实现bind函数
发布日期:2021-05-09 16:00:09 浏览次数:25 分类:精选文章

本文共 3460 字,大约阅读时间需要 11 分钟。

Function.prototype.bind���������JavaScript������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Function.prototype.bind������������������������������������������������������������������������������������������������������������������������

���������������������������������������������axios���������dom���������������������������������������������������������������Function.prototype.bind������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������

const a = { i: 2 };
function fun(x, y) {
alert(x + y);
alert(this.i);
}

���������������fun(1, 2)���������������������this������������������������������������������muon you {i: undefined}���������������������������������������fun���������������a������������

fun.bind(a)(1, 2)

������������������������������this������������������������a���������������������������������"3"���"2"���

���������������������������������������������������bind���������

function bind() {
const ob = this; // ���������������������������
const args = Array.from(arguments);
const fn = args[0]; // ������������������������
const boundArgs = args.slice(1);
// ���������������������������������������������������������������������
return function(newArgs) {
const allArgs = boundArgs.concat(newArgs);
return ob.apply(fn, allArgs);
};
}
Function.prototype.bind = bind;

������������������������������������������������������������������������������������������������������������������������������������������������������������bind������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������Web.php������������������������������������ moderne JavaScript������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������bind���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������bind���������apply���������������������������������������������������������������������������������������������������������bitron function������������newArgs���������������������������������������������������������������

������������������������������������������������������������������bind���������������������pnamic���������������������������������������������������������������

���������������������������������������������Function.prototype.bind������������������������ES6������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:对象新建属性对原型链的影响
下一篇:表单中的一些隐晦的bug

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月02日 14时04分01秒