var i;i.var x = 6x and what's in it is the number 6.x = x * 6function timesBySix(n) {
return n * 6;
}
So this cubbyhole has a name "timesBySix" and takes in another abstract cubbyhole thing called "n" ... and what it does is take whatever is put into it, calls it n, and does multiplication by 6 to it, and returns it.
var A = "I am variable A, in the global scope.";
var alpha = function() {
console.log(A); // "I am variable A, in the global scope."
}
var bravo = function() {
var A = "I am variable A, defined in bravo().";
var B = "I am variable B, defined in bravo().";
var charlie = function() {
console.log(A); // "I am variable A, defined in bravo()."
}
return charlie;
}
var delta = function() {
var A = "I am variable A, defined in delta().";
var B = bravo(); // returns the function charlie()
B(); // execute charlie()
}
alpha(); // "I am variable A, in the global scope."
var myCharlie = bravo(); // returns the function charlie()
myCharlie(); // "I am variable A, defined in bravo()."
// the instance of charlie() executed in delta() gets variable A
// from its own closure scope, NOT delta()'s scope.
delta(); // "I am variable A, defined in bravo()."
This can be perplexing at first, but it is actually a very useful language feature. There is not really such thing as a "global" scope in JavaScript, there is only an outermost scope. The same variable name can exist in multiple scopes, so which value it assumes depends on the context where it's retrieved.a = call_function(a, b); // assume this is in a function somewhere
int call_function(int c, int d)
{
int e = c + d;
return e;
}
and annotate it like this:
fprintf(stdout, "Calling call_function(a = %i, b = %i)\n", a, b);
a = call_function(a, b);
fprintf(stdout, "a has been set to the return value of call_function(), which is %i\n", a);
int call_function(int c, int d)
{
fprintf(stdout, "call_function(c, d) called: c = %i, d = %i\n", c, d);
int e = c + d;
fprintf(stdout, "New variable e (local to call_function()) declared!\n");
fprintf(stdout, "e is now c (%i) + d (%i), which is %i\n", c, d, e);
fprintf(stdout, "Now call_function is going to return the value of e, which is %i\n", e);
return e;
}
Then you could run it and have the program tell you what it was doing itself. If a had been set to 1 and b to 2, the output might look like this:
Calling call_function(a = 1, b = 2) call_function(c, d) called: c = 1, d = 2 New variable e (local to call_function()) declared! e is now c (1) + d (2), which is 3 Now call_function is going to return the value of e, which is 3 a has been set to the return value of call_function(), which is 3(apologies for using completely the wrong language, but hopefully the principle is apparent! From the above comments, it looks like console.log may be something you could use a bit like fprintf(stdout, ...))
You are not logged in, either login or create an account to post comments
posted by Mars Saxman at 9:56 AM on February 27