bind 仿造 重写bind
发布时间:2020-12-14 05:07:22 所属栏目:大数据 来源:网络整理
导读:简单版,不带参数 Function.prototype.my_bind = function(targ){ var _this = this; return function(){ _this.apply(targ) }}function f() { console.log(`${this.name}`);}var o = { name: ‘liu‘};f.my_bind(o)(); 带参数 Function.prototype.my_bind =
简单版,不带参数 Function.prototype.my_bind = function(targ){ var _this = this; return function(){ _this.apply(targ) } } function f() { console.log(`${this.name}`); } var o = { name: ‘liu‘ }; f.my_bind(o)();
带参数 Function.prototype.my_bind = function(){ var _this = this; var targ = Array.prototype.shift.call(arguments) var arg = Array.prototype.slice.call(arguments) return function(){ _this.apply(targ,Array.prototype.concat.apply(arg,arguments)) } } function f(a,b,c) { console.log(`${this.name} ${a} ${b} ${c}`); } var o = { name: ‘liu‘ }; f.my_bind(o,1,2)(3); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |