0

I was reading about the Function() constructor in JavaScript from the link here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

And it mentions the fact that :

Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was created

let count = 0; // var count = 0 works!

function makeCounter() {
  let count = 0;

  return new Function('return count++;');

}

let counter = makeCounter();

console.log( counter() ); // "ReferenceError: count is not defined 

In my example above, isn't the statement let count = 0 above the function makeCounter() in global scope?

I tried reading about how scoping works and found this Any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code

here : https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/

Turns out let count = 0 fits the definition of a global scope but still gives and error!

EDIT : I understand how scoping works, was more concerned about how Function() works considering it accesses variables from the global scope as given in definition.

1 Answers1

0

To answer the question in the title: "What exactly is global scope in JS? "

It depends on where the javaScript is being run.

Traditionally it means the "window Object", the outtermost layer of scope, with closures in the form of functions and objects being inner levels of scope (scope constrains variables).

Scope is like nesting of html elements.

let count = 0; // we only use "let" the first time we declar a variable

function makeCounter() { 
  let count = 0; // okay, so this `count` is completely different than the first one
}
function makeCounter( count ) { // and it will even get more confusing of we try to pass a variable to the funtion
  count = 0; 
}

does this maybe help?

let count = 0; // we only use "let" the first time we declar a variable

function makeCounter( current_count ) {
  return current_count++;
}

for ( let i = 0; i < 10; i++ ) {
  count = counter( count );
  console.log( count );
}
admcfajn
  • 2,013
  • 3
  • 24
  • 32
  • `I think there's a typo in your question & you meant "their own local variables and global once", not "their own local variables and global ones".` I copied the statement give in the MDN itself ... Also, is it right to say let count defined outside to be a global variable ? Since, it is accessible throughout the code ? – Gagan Ganapathy Ajjikuttira May 13 '20 at 09:53
  • It's a best practaice not too. a pattern like: `let thingamajig = { vars: { count: 0 }, makeCounter : function( current_count ) { return current_count++ } } for ( let i = 0; i < 10; i++ ) { count = counter( thingamajig.count ) console.log( count ) }` might be better – admcfajn May 13 '20 at 09:59