最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

[function.call()返回此全局值

运维笔记admin7浏览0评论

[function.call()返回此全局值

[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本身的支持。

发布评论

评论列表(0)

  1. 暂无评论