javascript - js中.call(underfined)中this指向什么?
ringa_lee
ringa_lee 2017-04-11 12:53:37
[JavaScript讨论组]

应该是指向undefined吧?

ringa_lee
ringa_lee

ringa_lee

全部回复(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说的一致

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号