这篇文章主要介绍了使用 js 简单的实现 bind、call 、aplly代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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);