javascript - 为什么“this”关键字总是在 node.js 中返回 undefined?
问题描述
每个人我都是 JS 的初学者。我读过文章。据我所知,“this”关键字是一个全局对象。如果我们使用“strict”,那么它将是未定义的。.为什么我在javascript浏览器和Node.js版本中得到不同的输出。有人可以解释一下吗?
scenario1:
(Javascript Browser):
var count=5;
console.log(count); //5
console.log(this.count); //5
(Node.JS 12.13.0)
var count=5;
console.log(count); //5
console.log(this.count); //undefined
scenario2:
(Javascript Browser):
function ghost() {
console.log(this.boo);
}
ghost(); // ABC
var boo = 'ABC';
ghost(); //ABC
(Node.JS 12.13.0)
function ghost() {
console.log(this.boo);
}
ghost(); // undefined
var boo = 'ABC';
ghost(); //undefined
解决方案
推荐阅读
- python - 如何组合张量以获得维度+1
- github - 为什么 GitHub 会创建一个 .gitignore 文件夹?错误或功能?
- java - 需要使用 java Regex 获取特定的单词
- typescript - 从 TypeScript 中的 promise 块中的函数推断类型
- segmentation-fault - 没有(核心转储)的分段错误
- highcharts - 需要在 Highcharts 中沿 x 轴的图例上的数据表
- julia - Julia 问题:在向量中查找 n 最小值的索引
- java - Log4j2 + Gradle - 包属性未检测到插件
- css - 为什么绝对定位与第二个div的文本重叠
- laravel - Laravel Nova 限制 indexQuery 中的结果