简体   繁体   English

在JavaScript中访问方法函数内的变量

[英]Accessing a variable inside a method's function in JavaScript

I'm a novice at JavaScript so please forgive any incorrect terminology/understanding. 我是JavaScript的新手,因此请原谅任何错误的术语/理解。

I'm trying to extract a variable thebest from a callback function function(thebest,all) within a method ec.get . 我试图提取变量thebest从一个回调函数function(thebest,all)的方法中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. 我已经对范围进行了一些阅读,并且希望下面的代码能够正常工作,但是似乎函数外的thebest变量与方法函数内的变量在不同的范围内。

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? 如何从方法及其函数外部访问“最内层” thebest变量的值? 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. 问题1:如果要在回调函数中更改theBest的值,则无法通过将其作为参数传递来进行更改。 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. 问题2:假设ec.get()是一个网络操作,它可能是异步的,这意味着您传递给它的回调函数要等到很久以后才被调用。 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. 问题3:您无法以声明的方式将参数传递给回调。 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. 这将定义这些参数,但是除非ec.get()像这样传递参数,否则在调用该参数时,这些参数实际上就不会存在。 Remember, it's ec.get() that calls your function internally. 请记住,是ec.get()在内部调用您的函数。 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. 问题4:当您为函数声明一个与局部变量或全局变量thebest参数(在示例中为最佳)时,就会造成名称冲突,导致该参数在函数范围内接管该名称,并使较高级别的变量不可访问。 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. 您的问题是您要重新声明theBest作为函数的参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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