2017-06-29 44 views
1

如下面的代码功能:什么是Node.js的文件绑定到node.js的文件

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

码结果object

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

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

在此先感谢!

+1

这是全局对象就像在浏览器,也可以通过'global'访问。 – Ryan

回答

0

当没有使用strict mode时,this将是所有模块共享的全局对象。

node js1.js应该在下面的例子打印真:

// 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;