Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'this' keyword behaves differently in Nodejs and browser

I have this piece of code :

var obj1; var obj2;  function x() {     obj1 = this; }  function y() {     obj2 = this; }  x(); y();  console.log(obj1 === obj2); console.log(obj1 === this); 

I ran this code in NodeJS using command line : node app.js and ran as a script in Chrome browser

The result : in NodeJS, the result was : true false NodeJS result

In Chrome browser, the result was : true true Browser result

How can this happened ? can anyone explain what really going on under the hood ?

like image 664
dandoh Avatar asked Jan 17 '16 13:01

dandoh


People also ask

What is the difference between NodeJS and browser?

Unlike the browser where Javascript is sandboxed for your safety, node. js has full access to the system like any other native application. This means you can read and write directly to/from the file system, have unrestricted access to the network, can execute software and more.

What are environment difference between node JS vs JavaScript in browser?

NodeJS is a cross-platform and opensource Javascript runtime environment that allows the javascript to be run on the server-side. Nodejs allows Javascript code to run outside the browser. Nodejs comes with a lot of modules and mostly used in web development. 2.

Does node JS have the same objects as browser based js?

Both the browser and Node. js use JavaScript as their programming language. Building apps that run in the browser is a completely different thing than building a Node. js application.


2 Answers

In the browser, running in the global scope, this is always window in your example

var obj1; var obj2;  function x() {     obj1 = this; // window }  function y() {     obj2 = this; // window }  x(); y();  console.log(obj1 === obj2);  // window === window = true console.log(obj1 === this);  // window === window = true 

This is not how it works in Node. In Node.js all modules (script files) are executed in their own closure while browsers execute all script files directly within the global scope.

In other words, in just about any file running in Node, this will just be an empty object, as Node wraps the code in an anonymous function that is called immediately, and you'd access the global scope within that context with GLOBAL instead.

This is also mentioned in the Globals documentation:

Some of these objects aren't actually in the global scope but in the module scope - this will be noted.

However, when calling a function without a specific context in Node.js, it will normally be defaulted to the global object - The same GLOBAL mentioned earlier, as it's execution context.

So outside the functions, this is an empty object, as the code is wrapped in a function by Node, to create it's own execution context for every module (script file), while inside the functions, because they are called with no specified execution context, this is the Node GLOBAL object

In Node.js you'd get

var obj1; var obj2;  function x() {     obj1 = this; // GLOBAL }  function y() {     obj2 = this; // GLOBAL }  x(); y();  console.log(obj1 === obj2);  // GLOBAL === GLOBAL = true console.log(obj1 === this);  // GLOBAL === {} = false 

Where the last this is indeed an empty object, as explained above


For completeness, it's worth noting that in strict mode, you'd get the same result in a browser (true, false) as in Node, but that's because the variables are just the opposite of what they are in Node

"use strict"  var obj1; var obj2;  function x() {     obj1 = this; // undefined }  function y() {     obj2 = this; // undefined }  x(); y();  console.log(obj1 === obj2);  // undefined === undefined = true console.log(obj1 === this);  // undefined === window = false 

This is because the value passed as this to a function in strict mode is not forced into being an object (a.k.a. "boxed").
For a normal function in non-strict mode, this is always an object, and it's always the global object if called with an undefined or null this-value, i.e. without a specific execution context.

Not only is automatic boxing a performance cost, but exposing the global object in browsers is a security hazard, because the global object provides access to functionality that "secure" JavaScript environments must restrict.

Thus for a strict mode function, the specified this is not boxed into an object, and if unspecified, this will be undefined inside functions, as shown above, but this will still be the window in the global scope.

The same thing happens in strict-mode in Node.js, where this inside the functions is no longer GLOBAL but undefined, and this outside the functions will still be the same empty object, and the end result will still be true, false, but the value of this will be different in strict-mode in Node.js as well.

like image 184
adeneo Avatar answered Sep 16 '22 21:09

adeneo


Node explicitly sets this to the module exports here:

const result = compiledWrapper.apply(this.exports, args); 

What apply does is explicitly fixate the this value (and parameters) - in this case - it sets it to this.exports. For example you can do:

(function() { console.log(this.x); }).apply({x:3}); // alerts 3 

Node override's the default behavior. It has to, however, call functions inside the object with global - as is mandated by the JS spec.

like image 25
Benjamin Gruenbaum Avatar answered Sep 16 '22 21:09

Benjamin Gruenbaum