[function.call()返回此全局值
我正在尝试使用function.call
方法将用户对象绑定为此,并将默认时间作为第一个参数绑定>
let user = { name:'rifat', txt (time, msg){ console.log('['+time+ '] '+ this.name+ ' : '+ msg); } } function bind(func, ...fArgs){ return function(...args){ return func.call(this, ...fArgs, ...args); }.bind(this); } let txt = bind(user.txt, new Date().getHours()+':' +new Date().getMinutes() ); txt('hey!');
为什么此代码返回未定义的名称。在节点10.16.0.0中运行
[18:21] undefined : hey!
我正在尝试使用function.call方法将用户对象作为此参数,并将默认时间作为第一个参数进行绑定。let user = {name:'rifat',txt(time,msg){console.log('['+ time + ']'+ this ....
回答如下:您要执行的操作实际上受本机bind
本身的支持。