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?

4
  • What did you get for the second? Commented Jul 11, 2012 at 4:07
  • Your console.log(window.jf) executes before the window.jf=func call and so it couldn't possibly output the jf method. Commented Jul 11, 2012 at 4: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. Commented Jul 11, 2012 at 4:08
  • Well the second jf = func; should "update" jf. What happens if you call jf(); after updating it? Commented Jul 11, 2012 at 4:13

2 Answers 2

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.

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

8 Comments

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.
Unfortunately that's the whole point of scopes & contexts, to isolate one part of the program from another.
shuuccks. I wonder if I could just replace the script tag.... Thank you very much grigore, very informative.
@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 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 stackoverflow.com/questions/9941736/…
|
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();

2 Comments

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—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.

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.