4

为什么node交互式里面的this和直接node js文件里的this不一样?

 2 years ago
source link: https://www.zhihu.com/question/478674602/answer/2052413855
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
为什么node交互式里面的this和直接node js文件里的this不一样?
登录后你可以
不限量看优质回答私信答主深度交流精彩内容一键收藏

js 文件在以 node index.js 这样的方式执行的时候,node对模块加载机制对其进行了一层包装

可以简单的理解为

function (){

index.js 的内容

所以这样方式执行所打印出来的 this就是该函数的this而非globalThis


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK