Reputation: 33
I have a quick question regarding some code I do not understand:
var anonymousGreet = function(){
console.log('Hi');
}
function log(a){
console.log(a);
}
log(anonymousGreet);
In the code above when I call the function log and pass in the function expression anonymousGreet as a parameter to log. Does that mean the variable "a" in log is pointing to the variable anonymous greet which is then pointing to the function object. Or does a point directly to the function object pointed to by anonymousGreet? I am a little confused if a first points to the variable anonymous greet or does it directly point to the function object pointed to by anonymousGreet. Sorry if this is confusing but any help would be appreciated! Thanks you!
Upvotes: 3
Views: 215
Reputation:
As a matter of terminology, and to avoid confusion, I would avoid the use of the word "pointer". It's too easy to get confused when using the word "pointer". Sometimes "A pointing to B" can mean that A is a kind of alias for B. Or that B can be modified through A. Or A can point to B which points to C; what does that mean?
IMO it is clearer to use the terminology that in JavaScript a variable holds a value. That's it. It holds a value. It does not point to anything, it does not refer to anything, it holds a value.
However, two variables may hold the same value, such as an empty array []
:
var a = [];
var b = a;
Both a
and b
hold the same value, a particular empty array. We can see that they hold the same value by checking a === b
. Since they hold the same value, that value can be manipulated through either one; a.push(1)
and b.push(1)
do exactly the same thing, because they are both manipulating the value held in common by each. Re-assigning to a
merely changes the value that a
holds; it does not and cannot affect b
, and vice versa. b
is not a "pointer" to a
, or an alias to a
; it is a variable which happens to hold the same value as a
.
In other words, a = b
does not make a
"point" to b
. Nor does it copy the value of b
and assign it to a
. It gives a
the value of b
, so that a
and b
now hold the identical value.
The function parameter passing mechanism can be thought of as a particular type of assignment. It makes the formal parameter in the function definition assume, or hold, the value of a parameter specified in the call. When I define a function
function foo(x) { console.log(x); }
and then call it
var y = 42;
foo(y);
the variable/parameter x
is assigned the value of the parameter passed in the function call. In other words, x
comes to hold the value of y
. x
does not "point" to y
, and it is not a copy of y
. It holds the same value as y
, which in this case is 42
.
Let's take your specific example:
var anonymousGreet = function(){
console.log('Hi');
}
function log(a){
console.log(a);
}
log(anonymousGreet);
In this case, anonymousGreet
is a variable which, by virtue of the assignment, holds as its value a particular anonymous function. When you call log(anonymousGreet)
, the parameter a
to the log
function is set to hold the value of the parameter in the call, so it now holds the value of anonymousGreet
. It does not "point" to the variable anonymousGreet
, and any changes to it cannot affect the value of anonymousGreet
, nor of course the function which that value represents.
Does that mean the variable
a
in log is pointing to the variableanonymousGreet
which is then pointing to the function object?
No, it means that the variable/parameter a
in log holds the value held by anonymousGreet
, which is the anonymous function.
Or does
a
point directly to the function object pointed to byanonymousGreet
?
Yes, but not "point to the thing pointed to by", but rather, "holds the value of anonymousGreet
, which holds as its value the anonymous function.
Upvotes: 1
Reputation: 114461
If you come from a C++ background then a simple rationalization is
For example when you write:
a = b + c;
you should imagine (in C++) something along the lines of
Object *a, *b, *c;
a = new Number(b->numericValue() + c->numericValue());
(note that however Javascript differently from C++ provides a garbage collector so no explicit delete
is ever needed).
This is of course just a simple description of what is the observable behavior (and it was may be the implementation of very first Javascript engines). Today what really happens behind the scenes is much more sophisticated and includes run-time generation of machine code (JIT).
This is the reason for which for example:
function foo(x) {
x[0] = 1;
}
var a = [0];
foo(a); // Will change the content of a[0]
but
function bar(x) {
x = 9;
}
var b = 0;
bar(b); // Will *NOT* change the content of b
Upvotes: 4
Reputation: 9412
Yes, if you check a===anonymousGreet, the result is true. All primitive values are passed by value, but objects are passed by reference in Javascript.
var anonymousGreet = function(){
console.log('Hi');
}
function log(a){
console.log(a === anonymousGreet); //true
console.log(a);
}
log(anonymousGreet);
Upvotes: 1