Javascript Function-Pointer Assignment

In other examples, nothing was passed by value; everything was passed by reference.

bar and foo are BOTH pointers

All vars/handles to NON primitive objects in javascript are pointers; pointers ARE native to javascript, they are the default.

var bar = function () { alert("A"); } //bar is a pointer to function1
var foo = bar;  //pointer copied; foo is now also a pointer to function1
bar = function () { alert("B"); };  //bar points to function2
foo();  //foo is still a pointer to function1

You will run into hidden errors and bugs if you think they are copies. Especially so if you work with complex objects. For example

function person(name){this.name = name}
var john = new person("john")
var backup = john
backup.name //john
john.name = "jack"
backup.name //jack, NOT john

To really COPY a non-primitive in javascript takes more work than just a = b. For example:

function person(name){  this.name = name}
var john = new person("john")
var backup = new Object()
backup = JSON.parse(JSON.stringify(john))
backup.__proto__ = john.__proto__   //useful in some cases
john.name = "jack"
backup.name //john

Yes that is expected and by design.

Your question is basically: does foo reference bar as a pointer or reference would in another language?

The answer is no: the value of bar at the time of assignment is assigned to foo.


I'm a bit late here but I thought I'd give an answer anyways and flesh something out.

It's best not to think in terms of pointers and memory references when discussing the internals of JavaScript (or ECMAScript) when dealing with the specifications. Variables are environment records internally and are stored and referenced by name, not memory address. What your assignment statement is doing, internally and by design, is looking up the environment record name (either "foo" or "bar") and assigning the value to that record.

So,

var bar = function () { alert("A"); }

is assigning the environment record "bar" the value (anonymous function).

var foo = bar;

internally calls GetValue("bar") which retrieves the value associated with the record "bar" and then associates that value with the record "foo". Hence, afterwards the original value of bar can still be used as it's now associated with foo.

Because JavaScript references by string and not memory address is precisely why you can do things like this:

someObject["someProperty"]

which is looking up the value based on the property name.


You are assigning the value of an anonymous function to a variable not a pointer.
If you want to play with pointers, you can use objects that are passed by reference, not copy.

Here are some examples:

"obj2" is a reference of "obj1", you change "obj2", and "obj1" is changed. It will alert false:

var obj1 = {prop:true},
    obj2 = obj1;
obj2.prop = false;
alert(obj1.prop);

"prop" points to a property that is not an object, "prop" is not a pointer to this object but a copy. If you change "prop", "obj1" is not changed. It will alert true:

var obj1 = {prop:true},
    prop = obj1.prop;
prop = false;
alert(obj1.prop);

"obj2" is a reference to the "subObj" property of "obj1". if "obj2" is changed, "obj1" is changed. It will alert false:

var obj1 = {subObj:{prop:true}},
    obj2 = obj1.subObj;
obj2.prop = false;
alert(obj1.subObj.prop);

Yes, there's nothing special about the fact that the variables are referring to functions, there's no aliasing involved.

var bar = 1;
var foo = bar;
bar = "something entirely different";
// foo is still 1