1

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?

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. 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. Commented Feb 19, 2014 at 17:16
  • I think prototype is good choice for this, is not? Commented Feb 19, 2014 at 17:16
  • @RocketHazmat Yes [limit] Commented Feb 19, 2014 at 17:18
  • @NathanP.: Ah, I see now. :) Commented Feb 19, 2014 at 17:19

3 Answers 3

5

Would 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:

enter image description here

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:

enter image description here

After line 3, you have reassigned b to something else (a new function), but a is still pointing to the original function:

enter image description here

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.

Sign up to request clarification or add additional context in comments.

7 Comments

According to you 'a is still pointing to the original function:' then why last line alerts 20?
It shouldn't be alerting 20 anywhere; the code is only alerting the string literals a or b.
Ok, I understand. But is there any way to accomplish what I want, more than one variable pointing to the same function, In such a way that when I update one, the other changes?
@Marcus Yes, you can do this by wrapping the function in another and then changing the inner function. I will post some code.
@VivinPaliath I was with your exmaple not with OP.
|
1

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');};

Comments

0

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 common underlying reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.