igobivo
igobivo

Reputation: 432

global scope in chrome and node.js

When I enter code console.log(this); in Chrome dev tools, it displays:

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:

{}

Shouldn't it display information about global object?

(Said code is only thing that is executed, it is not part of a program.)

Upvotes: 0

Views: 337

Answers (1)

Ivan Minakov
Ivan Minakov

Reputation: 1442

In the top-level code in a Node module, this is equivalent to 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).

Upvotes: 1

Related Questions