node.js文件中绑定的函数是什么

时间:2017-06-29 02:48:50

标签: javascript node.js

与node.js文件中的以下代码类似:

function test() {
    console.log(typeof this)
}

代码结果为object

我知道如果代码在浏览器中运行,则此函数绑定到默认的window对象。显然结果是对象

但是node.js文件中绑定的函数是什么?

提前感谢!

1 个答案:

答案 0 :(得分:0)

如果未使用strict modethis将成为所有模块共享的全局对象。

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;