3

I am in a position where I need to "update" a function that exists in another javascript file. The file looks like this:

function jf(){
    alert('1');
}

//call jf periodically
 jf();   

The second js file, which is loaded after looks like this:

console.log(jf);
console.log(window.jf);
var func=function(){
  alert('2');  
};
jf=func;
window.jf=func;

The first log successfully returns the original jf method, the second doesnt. The first set seems to set the local variable jf, and the second does basically nothing. Is there a way to achieve this functionality?

Dested
  • 6,294
  • 12
  • 51
  • 73
  • What did you get for the second? – xdazz Jul 11 '12 at 04:07
  • Your `console.log(window.jf)` executes before the `window.jf=func` call and so it couldn't possibly output the jf method. – Bill Jul 11 '12 at 04:07
  • second is undefined. The original `function jf()` executes long before either console.log. The window log checks to see if that function exists in the window scope. – Dested Jul 11 '12 at 04:08
  • Well the second `jf = func;` should "update" `jf`. What happens if you call `jf();` after updating it? – Anthony Mills Jul 11 '12 at 04:13

2 Answers2

2

According to Javascript closures - behavior of overridden functions from the global scope

var done = and function done do basicaly the same thing. They will shadow the outer definition in the inner scope but they will not replace it on the outer scope.

This means you can only override your initial definition of function jf() if you are in the same execution context. Otherwise, replace function jf(){ ... with window.jf = function(){...

Also, running your tests in an inspector console might help.

Community
  • 1
  • 1
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • Yeah... This is what I was looking for. And for the record, there is no way to simulate this context? The context of a completely separate js file that is. – Dested Jul 11 '12 at 04:19
  • Unfortunately that's the whole point of scopes & contexts, to isolate one part of the program from another. – Silviu-Marian Jul 11 '12 at 04:26
  • shuuccks. I wonder if I could just replace the script tag.... Thank you very much grigore, very informative. – Dested Jul 11 '12 at 04:27
  • @GRIGORE-TURBODISEL—I think you're confused. Each script element doesn't have a new scope, functions and windows do. The OP's code is all running in the one global context. The issue is one of execution order—assignment of a new function expression to (global) `jf` occurs after calling `jf`, so clearly the new function hasn't been assigned yet so it isn't called. Incidentally, `jf` and `window.jf` refer to exactly the same property of the global object. – RobG Jul 11 '12 at 05:40
  • @RobG the symptoms OP described suggest that `jf()` wasn't defined globally. If it was, the symptoms would've been different and it's all been covered in the reference I gave. And you should also see http://stackoverflow.com/questions/9941736/why-javascript-doesnt-let-a-function-redefine-itself-from-within-itself – Silviu-Marian Jul 11 '12 at 05:49
  • I don't see how the referenced question has anything to do with different scopes (though it does have to do with execution order). The OP seems to have been given the impression that each script element or file has a different execution context, which is completely wrong. The issue has nothing at all to do with execution context. Nor is there anything in the OP, comments or other answers to indicate that the code is in function scope. – RobG Jul 11 '12 at 06:59
  • @RobG Look at the results he's getting for `console.log(jf);` and `console.log(window.jf);`. – Silviu-Marian Jul 11 '12 at 11:15
  • Haha, I see. Dested did not mention that the first is running within an immediately-invoked function or something. Well-sleuthed my good man! :) – Anthony Mills Jul 11 '12 at 13:42
1

First, use variables:

var jf = function () {
    alert('1');
};

jf();

Then the second bit should work fine:

var func = function () {
    alert('2');
};

jf = func;
jf();
Anthony Mills
  • 8,676
  • 4
  • 32
  • 51
  • In a perfect world, yes. Unfortunately that's not an option. I'm not asking for best practices, just wondering if this one case is possible. – Dested Jul 11 '12 at 04:09
  • @Dested—the new value of `func` isn't assigned until after you attempt to call it. It has **nothing at all to do with scope** and everything to do with execution order. It is all running in the one global scope. – RobG Jul 11 '12 at 05:36