Javascript: Re-assigning a function with another function

Let’s say I have these two functions:

function fnChanger(fn) {
    fn = function() { sys.print('Changed!'); }
function foo() {

Now, if I call foo(), I see Unchanged, as expected. However, if I call fnChanger first, I still see Unchanged:

foo(); //Unchanged

Now, I assume this is because foo is not being passed to fnChanger by reference, but I may be wrong.

Why does fnChanger not change foo to print Changed!?
Furthermore, how can I get fnChanger to change foo without too much messy syntax?

PS: I’m using node.js to test all this stuff, so that’s where the sys.print comes from.

51 thoughts on “Javascript: Re-assigning a function with another function”

Leave a Comment