Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this & object prototypes - chapter 2 - example code seems wrong #1156

Closed
MostafaNasiri opened this issue Sep 30, 2017 · 12 comments
Closed

this & object prototypes - chapter 2 - example code seems wrong #1156

MostafaNasiri opened this issue Sep 30, 2017 · 12 comments

Comments

@MostafaNasiri
Copy link

When I run the code below:
function foo() { console.log(this.a); } var a = 2; foo(); // 2
I get "undefined" in my console but the book says it should print 2.
Where is the problem?

@getify
Copy link
Owner

getify commented Sep 30, 2017

I dunno... works for me:

screen shot 2017-09-30 at 7 09 56 am

@MostafaNasiri
Copy link
Author

Weird! When I run this code in chrome's web console it gives me the correct output but when I run it with nodejs compiler then I get "undefined" instead of 2!

@getify
Copy link
Owner

getify commented Sep 30, 2017

Node's "global scope" is not a purely standard JS environment and cannot be trusted to give you accurate JS behavior like this.

@bmeck
Copy link

bmeck commented Sep 30, 2017

CommonJS wraps .js files in a boilerplate function. See https://nodejs.org/api/modules.html#modules_the_module_wrapper

@MostafaNasiri
Copy link
Author

I didn't know that. Thanks guys!

@getify
Copy link
Owner

getify commented Sep 30, 2017

@bmeck on that topic, I've always wondered why Node has to do that for the main .js file it's executing, as opposed to only doing it for any modules you load?

@bmeck
Copy link

bmeck commented Sep 30, 2017

@getify to inject things like require (scoped to file path and not a global) and module (process.mainModule === module) for people to use. Also prevents various differences like allowing top level return depending on how a module is loaded.

Node is also deciding to register this file type as a separate MIME : nodejs/TSC#371

@getify
Copy link
Owner

getify commented Sep 30, 2017

@bmeck doesn't node care that their deviations confuse JS devs? :)

IIRC the v8 API provides a way to inject identifiers into scope without having to wrap code in an IIFE, no?

@bmeck
Copy link

bmeck commented Sep 30, 2017

@getify doesn't have files be wrapped/not wrapped depending on how they are loaded just add to the confusion?

@bmeck
Copy link

bmeck commented Sep 30, 2017

it should be noted that injecting variables isn't sufficient, grammar/this value also changes

@getify
Copy link
Owner

getify commented Oct 1, 2017

grammar/this should never have been changed, that's my point, it perpetually confuses developers, like in this thread.

@bmeck
Copy link

bmeck commented Oct 1, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants