-
Notifications
You must be signed in to change notification settings - Fork 33.6k
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
Comments
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! |
Node's "global scope" is not a purely standard JS environment and cannot be trusted to give you accurate JS behavior like this. |
CommonJS wraps |
I didn't know that. Thanks guys! |
@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? |
@getify to inject things like Node is also deciding to register this file type as a separate MIME : nodejs/TSC#371 |
@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? |
@getify doesn't have files be wrapped/not wrapped depending on how they are loaded just add to the confusion? |
it should be noted that injecting variables isn't sufficient, grammar/this value also changes |
grammar/this should never have been changed, that's my point, it perpetually confuses developers, like in this thread. |
That ship has long since passed and I was just explaining things.
…On Sep 30, 2017 10:39 PM, "Kyle Simpson" ***@***.***> wrote:
grammar/this should never have been changed, that's my point, it
perpetually confuses developers, like in this thread.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1156 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOUowyqwtBNjfkXtGu0WajAYvO97t2Rks5snwl1gaJpZM4PphB1>
.
|
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?
The text was updated successfully, but these errors were encountered: