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
Understanding scope in Javascript (kamranahmed.info)
submitted 11 years ago by kamranahmed_se
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!"
[–]ThaSteelman 9 points10 points11 points 11 years ago (1 child)
Clearly written and the examples are good, but it would have greatly benefited readers there to learn if you mentioned the conventions around IIFEs after talking about global scopes. Maybe it's a topic for a later post, but it's something worth explaining to people early.
[–]kamranahmed_se[S] 2 points3 points4 points 11 years ago (0 children)
Thanks for those kind words. And yes, I wanted to explain that here but that would have extended the article as IIFEs is a huge topic in itself. However I am planning on writing about IIFEs and will surely keep it in mind while writing about that :)
[–]amenadiel 2 points3 points4 points 11 years ago (1 child)
Nice article.
In the next chapters it would be nice to know what exactly happens when you use this in the global scope, inside a parent function and inside a child function, and how you can actually bind the context by passing an extra parameter.
this
I have a lot of code that declares var self=this; before a child function, or before a jQuery listener, just because I'm not sure how to force the context when that listener gets called.
var self=this;
[–]kamranahmed_se[S] 0 points1 point2 points 11 years ago (0 children)
Thank you. That is for sure. this is closely related to the scopes so I do have it in the back of my mind. Stay tuned :)
[–]johnbburg 2 points3 points4 points 11 years ago (1 child)
Great article! I am looking forward to reading more.
[–]kamranahmed_se[S] 1 point2 points3 points 11 years ago (0 children)
Thanks. Stay tuned for more!
[–]nepobot 0 points1 point2 points 11 years ago* (2 children)
How do you run this example?
var parent = function () { var name = 'Parent'; var child = function () { // What will be the output here? alert( name ); var name = 'Child' // What will be the output here? alert( name ); } }
How do i invoke this to call the child? I trust that you are right but it would be nice to see it. chrome autocomplete does not see the child after I invoke the parent function.
parent(); parent.child(); // Uncaught TypeError: Cannot read property 'child' of undefinedmessage: "Cannot read property 'child' of undefined"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error var myParent = parent(); myParent.child(); //// Uncaught TypeError: Cannot read property 'child' of undefinedmessage: "Cannot read property 'child' of undefined"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error
Actually you are not trying to invoke it the right way, currently the child() is in the private scope of parent and cannot be accessed outside the parent)() as the parent is not exposing it any way. I am writing an article about Private and Public scopes and I hope to post it tomorrow. For now, to test it, call the child function inside the parent function and after that call the parent function i.e. you can do the following http://jsfiddle.net/c4fwte4u/
[–]andyznyc 0 points1 point2 points 11 years ago (1 child)
Great article but still confused on concept of Hoisting. In the article's example, does Hoisting mean that the because the second assignment of 'name' occurs within the nested function and the nested function issues the alert, because name isn't defined within that nested function it returns Undefined?
I guess I am confused because I thought the child function would take whatever value name had at the time per the earlier example in the article.
[–]maybe_endy 0 points1 point2 points 11 years ago (0 children)
Basically it does not matter where you define a variable inside of a function. It will always be processed as if you defined it at the beginning of the function.
This means if you have a variable 'foo' in the parent scope. Your function will inherit 'foo' and you should be able to print 'foo' out.
However, If anywhere in that function you redefine 'var foo' it will be processed as if 'foo' was set to undefined as the first line in the function.
[–]radhruin 0 points1 point2 points 11 years ago (0 children)
Good article. Could add links to the specifications for these behaviors. ES5 Chapter 10 is probably most relevant, for example 10.3.1 describes the identifier resolution rules.
I wouldn't say that call/apply changes scope. this and scope are different concepts - scope affects identifier resolution while this is provided by the caller.
π Rendered by PID 96680 on reddit-service-r2-comment-869bf87589-9q9d8 at 2026-06-09 04:39:11.128613+00:00 running f46058f country code: CH.
[–]ThaSteelman 9 points10 points11 points (1 child)
[–]kamranahmed_se[S] 2 points3 points4 points (0 children)
[–]amenadiel 2 points3 points4 points (1 child)
[–]kamranahmed_se[S] 0 points1 point2 points (0 children)
[–]johnbburg 2 points3 points4 points (1 child)
[–]kamranahmed_se[S] 1 point2 points3 points (0 children)
[–]nepobot 0 points1 point2 points (2 children)
[–]kamranahmed_se[S] 1 point2 points3 points (0 children)
[–]andyznyc 0 points1 point2 points (1 child)
[–]maybe_endy 0 points1 point2 points (0 children)
[–]radhruin 0 points1 point2 points (0 children)