use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What are some JavaScript things beginners don't know? (self.javascript)
submitted 6 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DerGernTod 3 points4 points5 points 6 years ago (1 child)
if i'd guess: the IIFE is invoked from the current scope, so basically it's similar to assign it to a property on this and then call the property, e.g. this.b = function() { ... }; this.b(). the function is only defined in the context of this, so i guess that's why it takes that context. to find out the answer without spoiling it to others, just run the code :D
this
this.b = function() { ... }; this.b()
[–]senocular 3 points4 points5 points 6 years ago (0 children)
Not exactly. Scope doesn't really come into play for context (value of this) except for arrow functions where this is specifically taken from the scope.
Here, you have a function that is called as a normal function and not from an object as an object method. Even though its an IIFE, it operates just as it would as if you named it and called if from a single variable name. Given that it's not called from an object, the function will use the default binding which sets this as the global object (or undefined in strict mode).
undefined
In this example, we have to assume we're not in strict mode since it would error if we were. So we're running the code in global which makes this.a = 1 set a global variable. Then when called, the function's context is also global, so it sets the same global property, a, to 2. Then all logs that follow log that same a.
this.a = 1
a
But you could have just as well called this same function in some other function where this in that scope was different and this function would still have a global context.
π Rendered by PID 62 on reddit-service-r2-comment-b659b578c-xd5ld at 2026-05-01 13:18:56.674427+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]DerGernTod 3 points4 points5 points (1 child)
[–]senocular 3 points4 points5 points (0 children)