js手动实现bind教程
程序员文章站
2022-07-07 20:49:58
function.prototype.bind=function(obj){
var arg=array.prototype.slice.call(arguments...
function.prototype.bind=function(obj){ var arg=array.prototype.slice.call(arguments,1); var context=this; var bound=function(){ arg=arg.concat(array.prototype.slice.call(arguments)); return context.apply(obj,arg); } var f=function(){} f.prototype=context.prototype; bound.prototype=new f(); return bound; } function read(name, time, book) { console.log(`${name} is reading ${book} at ${time}`) } var tomread = read.bind(this, 'tom', 'morning') tomread('<万历十五年>') console.log(tomread.bind)