js中.call(underfined)中this指向什么?

应该是指向undefined吧?

阅读 2.5k
2 个回答

不对,如果在浏览器是指向window, Node中是global

function sayThis() {
  console.log(this);
}
sayThis.call(undefined); // Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…}

注意:当第一个参数为nullundefined时,将是JS执行环境的全局变量。浏览器中是window,其它环境(如node)则是global。

The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object and primitive values will be converted to objects.

摘自MDN,已经解释得很清楚了。和theWalker说的一致

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题