August 26, 2008

Are functions passed by reference or value


All the documentation i can find  (ex: msdn jscript documents ) says functions, like other objects, are assigned to variables by reference.



Code:


foo = new Function ('var u="*************";');

var bar = foo; // creates a ref or a copy ?

foo = new Function ('var u="_____________";'); //clobber foo

alert( [  foo.toString(),  bar.toString()  ].join("\n\n"))



this alerts:

Code:
function anonymous() { var u = "_____________"; } 
function anonymous() { var u = "*************"; }



if foo was assigned to bar by reference, why does bar not reflect the current value of foo?

is there a rule or convention that controls when functions are passed by value -vs- when they are passed by reference ?