2

NOTE : This is just out of curiosity, not a blocker for me as such.

while i was working on my NODE project, i hit something which actually confused me and i could not find why its so. please find sample code to understand the question

function a() {
    console.log(this === GLOBAL);  //true
}    
console.log(this === GLOBAL);      // false
a();

Now, in node documentation it clearly says

The top-level scope is not the global scope

so i understand from above note why this is false outside the function a(top-level). but then inside function a this is pointing to GLOABAL, why is that ?

i am using node-5.5.0, but i checked the behavior on node-0.12, its consistent

may be its some stupid misunderstanding from my side, bear with me.

UPDATE: by the way - this in top-level is module.exports, and this inside the function is GLOBAL

Community
  • 1
  • 1
Oxi
  • 2,918
  • 17
  • 28
  • 1
    You are confusing two different concepts. The value of `this` has nothing to do with scope. – Quentin Feb 05 '16 at 12:07
  • @Quentin : i was trying to find a name for "JS code outside of any function" (like global scope in browser), in this question i only mean the context in different scope. editing the question – Oxi Feb 05 '16 at 12:11
  • Have a look at [In what scope are module variables stored in node.js?](http://stackoverflow.com/q/15406062/1048572) – Bergi Feb 05 '16 at 12:22
  • "*The top-level scope is not the global scope*" makes not much sense, as the global scope is by definition the top-level one. They probably mean "the scope in which module code is executed is not the global one", contrasted to the scope of scripts in a browser. – Bergi Feb 05 '16 at 12:24
  • @Bergi : Thats from nodejs documentation : https://nodejs.org/api/globals.html#globals_global – Oxi Feb 05 '16 at 12:26
  • @Bergi : i get it now, http://stackoverflow.com/q/15406062/1048572 was helpful . Thanks – Oxi Feb 05 '16 at 12:35

2 Answers2

1

Your question is not related to Node per se but to the ECMAScript spec itself. You might want to read about Lexical Environments and Execution Contexts.

this is only global in the function call because you’re in non-strict mode; if you were to use the 'use strict'; pragma, this would be undefined.

The MDN gives some insight:

First, 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, this is always an object: either the provided object if called with an object-valued this; the value, boxed, if called with a Boolean, string, or number this; or the global object if called with an undefined or null this. (Use call, apply, or bind to specify a particular this.) 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

So, in non-strict mode, this inside a function will default to global.


Under the hood, Node modules are wrapped in a function call, which will give you access to the exports, require, module, __filename and __dirname variables:

(function (exports, require, module, __filename, __dirname) {
  // your actual code will be injected here
});

This function is run using exports as the context (i.e. this).

Iso
  • 3,148
  • 23
  • 31
0

In JavaScript, the value of this can cause lots of confusion. I'll try to explain just the bits that are relevant here:

  • in a 'standalone' function, this becomes the global object
  • in a method (i.e. a function that is attached to an object) this becomes the object whose method is being called

eg.

function a() { console.log(this) }
a() //-> logs the global object (window in the browser, global in node)

var obj = { a: function () { console.log(this) } }
obj.a() //-> logs 'obj'

Your observations of this being set to global are not unique to node and is true in the browser also. There is a thing in JavaScript called "strict mode" which when enabled makes the value of this=undefined in the situation we're talking about, which is a much more sensible value.

This article is very informative if you want to understand more about this: http://www.2ality.com/2014/05/this.html.

Ben Gourley
  • 129
  • 3
  • 1
    OP seems to ask about `this` when *not* in a function. – Bergi Feb 05 '16 at 12:39
  • @Ben : well my question was mostly about `this` being `module.exports` in nodejs(not applicable to browser), in `strict mode` or `non strict mode`. anyway, the link Berge provided and some other read clears it. Thanks – Oxi Feb 05 '16 at 12:49
  • @Bergi I figured this was the crux of the question "but then inside function a this is pointing to GLOABAL, why is that" :) – Ben Gourley Feb 05 '16 at 13:37