I am trying to change the definition of a function:
var a = function(){alert('a');};
var b = a;
b = function(){alert('b');};
This results in the variable a
keeping it's original assignment i.e. the function producing alert('a')
.
Is there any way of passing around a reference to a javascript function so that I can alter it later?
I am trying to change the definition of a function:
var a = function(){alert('a');};
var b = a;
b = function(){alert('b');};
This results in the variable a
keeping it's original assignment i.e. the function producing alert('a')
.
Is there any way of passing around a reference to a javascript function so that I can alter it later?
Share Improve this question edited Feb 19, 2014 at 17:19 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Feb 19, 2014 at 17:14 MarcusMarcus 971 gold badge2 silver badges8 bronze badges 6- 3 No, you really can't do this with a simple variable. You can keep a function reference as an object property, and then manipulate that property value. – Pointy Commented Feb 19, 2014 at 17:15
- @RocketHazmat, that's not what he wants. He wants to overwrite A with B on the third line. – Prisoner Commented Feb 19, 2014 at 17:16
- I think prototype is good choice for this, is not? – Suman Bogati Commented Feb 19, 2014 at 17:16
- @RocketHazmat Yes [limit] – Serge K. Commented Feb 19, 2014 at 17:18
- @NathanP.: Ah, I see now. :) – gen_Eric Commented Feb 19, 2014 at 17:19
3 Answers
Reset to default 5Would you expect the value of a
to change after the following snippet? Snippet in question:
var a = 10;
var b = a;
var b = 20;
No, right? So why would you expect reassigning b
to also affect a
?
After line 1
, you have a
pointing to a function instance:
After line 2
, you have a new variable b
, also pointing to the same function instance. So now you have two variables, both pointing to the same instance:
After line 3
, you have reassigned b
to something else (a new function), but a
is still pointing to the original function:
You can do what you want by doing something like this:
var func = function() { alert("a"); };
var a = function() { func(); };
var b = a;
func = function() { alert("b"); };
Now calling a()
or b()
will alert the string b
.
Is there any way of passing around a reference to a javascript function so that I can alter it later?
There is no way to do this. Javascript does not have "pointers". It has reference values, and as such, a
is a reference to the value of a, not to the memory location of a.
So, for this set of instructions
var a = function(){alert('a');};
var b = a;
b = function(){alert('b');};
this is the progression
//a is stored at some memory location
var a;
//b is stored at some memory location
var b;
//the memory location where a is stored has its value updated
a = function(){alert('a');};
//the memory location where b is stored has its value updated
//from the value stored at a's memory location
b = a;
//the memory location where b is stored has its value updated
b = function(){alert('b');};
You could produce the result you're looking for like this:
var fn = function() { alert('a'); };
var a = function() { fn(); };
var b = a;
fn = function(){ alert('b'); };
This code would produce the desired effect you're looking for because they'll both call fn() and you're changing the mon underlying reference.