-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Scope Problem is Confusing #125
Comments
Hey @michaelgrilo, this is really helpful! I agree that the IIFE stuff is distracting. I'll write a revised version and will post it here later to see what you think. |
Here's an attempt at a much simpler version of a Let me know what you think. SCOPE
JavaScript has two scopes: Functions defined inside other functions, known as nested functions, have access to their parent function's scope. Pay attention to the comments in the code below: /* global variables are created outside functions */
var hello = 'this is global';
/* local variables are created inside functions */
function changeGlobalVariable () {
var hi = 'this is local';
}
/* this will return 'this is global' */
console.log(hello);
/*
* this will return the error `ReferenceError: hi is not defined`,
* because `hi` is defined inside a function and is not available in the global scope
*/
console.log(hi); The challenge:Create a file named In that file, copy the following code: /* this is a global variable definition, because it is outside a function */
var aGlobalVariable = 'a global variable';
/*
* define a function with two local variable definitions,
* including usage of a function that returns a value
*/
function iAmAString () {
var me = 'i am a string'
var local = aLocalVariable();
}
/* define a function that returns a local variable */
function aLocalVariable () {
var local = 'a local variable';
return local;
}
/* this runs the iAmAString() function so that everything inside the iAmAString() definition is executed. */
iAmAString(); Use your knowledge of the variables' 'i am a string constructed from a local variable & a global variable' Your answer should include a combination of variables and strings concatenated together using the plus symbol Check to see if your program is correct by running this command:
|
Do we need to add a semi-colon so the line reads Using the copied code, it's taking me too long to logically come to a solution. I can only solve the problem with the following simplified code: var aGlobalVariable = 'a global variable';
function iAmAString () {
var me = 'i am a string';
return me;
}
function aLocalVariable () {
var local = 'a local variable';
return local;
}
console.log(iAmAString() + ' constructed from ' + aLocalVariable() + ' & ' + aGlobalVariable); I get that |
As someone who has no previous JavaScript knowledge, the Scope problem is quite frustrating. Even though the introduction does a good job defining scope, I was not prepared to follow the code example.
Comments need to be added next to
console.log
to indicate that when we pass the argument withinbar
back to the function,var b = 2
will be ignored. I'm sure I haven't phrased that well, but it's a starting point.Also, what purpose does the line
foo(); // 4, 2, 48
serve? I understand that foo is the function's name, and the comments next to it are the values of these variables, but this statement is just sitting there. We need context. What would includingfoo();
at the end of this program do when we run it?I also don't believe the IIFE section adds any relevant information to this problem. It is distracting.
Now moving on to the challenge, I don't understand why there is
)();
after every closing}
. I suppose that ties back to thefoo();
in the example code, but I don't understand its usage.The text was updated successfully, but these errors were encountered: