简体   繁体   中英

Accessing a variable inside a method's function in JavaScript

I'm a novice at JavaScript so please forgive any incorrect terminology/understanding.

I'm trying to extract a variable thebest from a callback function function(thebest,all) within a method ec.get .

I've done a little reading up on scope and I was expecting the code below to work, but it appears the thebest variable outside the function is in a different scope than the variable inside the method's function.

var thebest = 0;
ec.get("id", function(thebest,all) { });
alert(thebest);

I have also tried using a different variable name on the outside but it made no difference. How can I access the value of the "innermost" thebest variable from outside the method and its function? Thanks!

It looks like there are several issues here:

Issue 1: If you want to change the value of theBest in the callback function, you can't change it by passing it as a parameter. Simple variables are passed by value so the original isn't changed if you change it in the function.

Issue 2: Assuming ec.get() is a networking operation, it's probably asynchronous which means that the callback function you pass it isn't called until much later. That means, the completion callback function will not have executed yet when your alert fires. So, it won't have changed anything yet.

Issue 3: You can't pass arguments to a callback the way you have it declared. That will define those arguments, but unless ec.get() is going to pass arguments just like that, the arguments won't actually be there when it's called. Remember, it's ec.get() that calls your function internally. It alone decides what arguments your callback gets. You can't change that.

Issue 4: When you declare an argument for your function with the same name as a local or global variable ( thebest in your example), you create a name conflict which causes the argument to take over that name for the scope of your function and make the higher level variable inaccessible. In general, it's a bad idea to name a function argument with the same name as any other variable that is in scope. It just asks for you or other people reading your code to get confused and make wrong assumptions about what is getting modified or read when using that name.

One way to do this is as follows:

var thebest = 0;
var all = "whatever";
ec.get("id", function() {
    // use the arguments "thebest" and "all" here which are available 
    // from the higher scope.  They don't need to be passed in
    alert(thebest);
    alert(all);
});
// you can't alert on the value of thebest and all here because 
// an asychronous callback won't have yet been called and 
// won't have yet modified those values

If the callback is something that executes promptly (not async) then you can simply assign it out to a differently named variable. For example

var theBest = 0;
ec.get("id", function(theBestArg,all) { theBest = theBestArg; });
alert(thebest);

Your problem is that you are redeclaring theBest as an argument for your function.

var thebest = 0;
ec.get("id", function(theNewBest,all) { theBest = 'new value' });
alert(thebest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM