与node.js文件中的以下代码类似:
function test() {
console.log(typeof this)
}
代码结果为object
我知道如果代码在浏览器中运行,则此函数绑定到默认的window对象。显然结果是对象
但是node.js文件中绑定的函数是什么?
提前感谢!
答案 0 :(得分:0)
如果未使用strict mode
,this
将成为所有模块共享的全局对象。
node js1.js
应在以下示例中打印为true:
// js1.js
const logT2 = require("./js2").logT2;
function logT1() {
return this;
}
const thisInT1 = logT1();
const thisInT2 = logT2();
console.log(thisInT1 === thisInT2 && typeof thisInT1 === "object");
// js2.js
function logT2() {
return this;
}
exports.logT2 = logT2;