使用 js 简单的实现 bind、call 、aplly
2019-05-17 17:14 muamaker 阅读(352) 评论(0) 编辑 收藏 举报Function.prototype._call = function(obj,...arg){ var me = this; var k = Symbol("test"); //生成唯一的 key obj[k] = me; var result = obj[k](...arg); delete obj[k]; return result; } Function.prototype._apply = function(obj,arg){ var me = this; var k = Symbol("test"); //生成唯一的 key obj[k] = me; var result = obj[k](...arg); delete obj[k]; return result; } Function.prototype._bind = function(obj,...arg){ var me = this; return function(...args){ var param = [...arg,...args]; return me._apply(obj,param); } } var o1 = { info:"o1", print(){ console.log(arguments) console.log(this.info) } } var o2 = { info:"o2", print(){ console.log(arguments) console.log(this.info) } } var o3 = { info:"o3", print(){ console.log(arguments) console.log(this.info) } } o1.print._bind(o3,11)(44,66);