3 ms·
This is not necessarily true: javascript passed arguments by reference, and thus g and s are actually "given back" to the caller Pass by reference doesn't work
by wladimir 15y ago
This is not necessarily true: javascript passed arguments by reference, and thus g and s are actually "given back" to the caller
Pass by reference doesn't work that way in JS;
function a(b) { b = 10; }
var foo;
a(foo);
alert(foo);
Won't display "10". It behaves like passing pointers, not mutable references such as the C++'s &.
- stingraycharles 15y agoI stand corrected, you are right. Then the only thing I can think about is that accepting it as an additional parameter in the function call is less overhead (as in, bytes) than defining 'b' as a var: function a(b) { b = 10; } is smaller than function a() { var b = 10; } and doesn't pollute the global namespace. EDIT: I see dave1010uk also said this.