[英]global scope in chrome and node.js
When I enter code console.log(this);
当我输入代码
console.log(this);
in Chrome dev tools, it displays: 在Chrome开发者工具中,它显示:
Window {stop: function, open: function, alert: function, confirm: function, prompt: function…}
but when I execute same code in node.js (v6.11.1) it displays: 但是当我在node.js(v6.11.1)中执行相同的代码时,它显示:
{}
Shouldn't it display information about global object? 它不应该显示有关全局对象的信息吗?
(Said code is only thing that is executed, it is not part of a program.) (所说的代码只是执行的东西,不是程序的一部分。)
In the top-level code in a Node module, this is equivalent to module.exports. 在Node模块的顶级代码中, 这等效于module.exports。 That's the empty object you see.
那是您看到的空对象。 For example:
例如:
module.exports.a = 'test';
console.log(this); // 'test'
In browsers, without using "use strict"
-directive, this refers to global object (Window). 在浏览器中,不使用
"use strict"
指令, 这是指全局对象(窗口)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.