【问题标题】:JavaScript array losing scope and not passing into my functionJavaScript 数组失去作用域并且没有传递到我的函数中
【发布时间】:2019-01-30 08:35:27
【问题描述】:

我正在创建一个数组作为 TAFFYDB() 查询的结果(但任何数组都可以)。

var ret=anaDataDB({"objID":"micro","objVal":1}).get();
console.log(ret.length);  // correctly shows 2
$("#submissionInput").fadeOut(function(){
  console.log(ret.length);  // correctly shows 2     
  $("#anaInputs").load("data/stubs/csaReport.html").promise().done(function(){
            console.log(ret.length);  // is undefined 

我尝试在函数调用中传递它

$("#submissionInput").fadeOut(function(){
    $("#anaInputs").load("data/stubs/csaReport.html").promise().done(function(ret){
        console.log(ret.length);  // shows 1 ??

我假设这是因为我使用了承诺或其他一些巫术。 为什么我失去了我的范围?

【问题讨论】:

  • regret 是不同的变量。
  • 什么是reg?将其替换为ret
  • 抱歉,打错了。在我的代码中到处都是“ret”
  • 在您发布代码后数组是否会发生变异?
  • 没有。我不得不手动重新输入一些代码以进行澄清并且有一个错字。我已经修好了。

标签: javascript arrays scope


【解决方案1】:

您确定不需要先将其传递给外部函数吗?像这样:

$("#submissionInput").fadeOut(function(ret){
    $("#anaInputs").load("data/stubs/csaReport.html").promise().done(function(ret){
        console.log(ret.length);  // shows 1 ??

【讨论】:

  • console.log(ret.length);在外部函数调用后正确显示 2
  • 对不起 - 你是对的。您正在超载 ret。换句话说,恰恰相反。删除传递给 done 方法的 ret 变量。例如$("#anaInputs").load("data/stubs/csaReport.html").promise().done(function(){
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2011-09-14
  • 2020-11-30
相关资源
最近更新 更多