如node.js文件中的以下代码:
function test() {
console.log(typeof this)
}
代码结果是对象
我知道如果代码在浏览器中运行,这个函数就会绑定(bind)到默认的窗口对象。显然结果是object
但是node.js文件中绑定(bind)的函数是什么?
提前致谢!
最佳答案
当不使用严格模式
时,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;
关于javascript - node.js 文件中绑定(bind)的函数是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815576/