Jump to content

question about local and global scope


TheCrock
 Share

Recommended Posts

I am very confused and I hope below makes sense.

The first time I ran these two functions - the first called globalScope, returned 'window'in the console and the second called  localScope, returned 'undefined' in the conole.

let globalScope = () => console.log(this);
globalScope();


let localScope = function(){
console.log(this);
};

localScope();

I played a little with the second function  and created a var called scope. var scope = this;

then used console.log(scope); and it returned 'window'.

 

Nice, So i cleared all of this and tried the two functions again and now they both return 'window' in the console.

Am i mistaken? Did i reset something in Chrome?

Shouldn't localScope return undefined again now.

I am working from a book it says:

■■Note If you’re using ES5, you’ll need to force a function expression to use local scope by adding
"use strict" as the function’s first line.

 

So here is my REAL NEWBIE question. How do I know if I am using ES5 or ES6?

Was the first time i ran the code using ES6 to get 'undefined' from localScope function -  and now I am using ES5 and need to "use strict"

I hope that makes sense because I am very confused.

The book says that the localScope function would return 'undefined' in the console log and it did. But now it is returning 'window' in the console log.

 

Thanks in advance Keith.

 

 

Edited by TheCrock
clarity
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...