【问题标题】:Javascript methods recalling and printingJavascript 方法调用和打印
【发布时间】:2012-06-26 13:52:25
【问题描述】:

我正在学习 JavaScript,但我在调用函数时遇到了一些问题......

这是我的两个功能: 第一个:

function geisson() {
    var iabile = new XMLHttpRequest();
    iabile.onreadystatechange = function () {
        if (iabile.readyState == 4) {
            var objectjson = {};
            var arrayCards = []; //creazione dell'array che conterrà le cards
            objectson = JSON.parse(iabile.responseText);
            arrayCards = objectson.cards;
            var Ettore = []; //Vèttore di cards

            //the results
            for (i = 0; i < arrayCards.length; i++)
                document.getElementById('image').src = "http://www.mysite.com/png/public/card/" + arrayCards[i].__guid__ + "?width=292";
        }
    }
    iabile.open("GET", "gnekcard.json", true);
    iabile.send(null);
}

第二个功能:

function Entity() {
    var iabile = new XMLHttpRequest();
    iabile.onreadystatechange = function () {
        if (iabile.readyState == 4) {
            var objectjson = {};
            var arrayCards = []; //creazione dell'array che conterrà le cards
            objectson = JSON.parse(iabile.responseText);
            arrayCards = objectson.cards;
            //the results
            for (i = 0; i < arrayCards.length; i++)
                document.getElementById('informazioni').innerHTML += "\r\n" + "Nome : " + arrayCards[i].__title__ + "\r\n" + "Vanity url: " + arrayCards[i].vanity_urls[0] + "\r\n";
        }
    }
    iabile.open("GET", "gnek.json", true);
    iabile.send(null);
}

我想要第三个函数来打印其他两个函数的结果。我宁愿只在第三个函数中使用“for”并回忆其他方法的向量,但它们不是全局的。我不想拥有全局变量(如果可能的话),我该怎么做?

【问题讨论】:

    标签: javascript function variables methods


    【解决方案1】:

    geisson 函数中,您可以这样做:

    geisson.arrayCards = arrayCards;
    

    你可以在Entity函数中做同样的事情

    Entity.arrayCards = arrayCards;
    

    然后您可以创建第三个函数,该函数可以访问每个函数的arrayCards

    function displayArrayCards {
      var geissonCards = geisson.arrayCards;
      var EntityCards = Entity.arrayCards;
      var i;
      for(i = 0; i < geissonCards.length; i++) {
        document.getElementById('image').src = "http://www.mysite.com/png/public/card/" + geissonCards[i].__guid__ + "?width=292";
      }
      for(i = 0; i < EntityCards.length; i++) {
        document.getElementById('informazioni').innerHTML += "\r\n" + "Nome : " + EntityCards[i].__title__ + "\r\n" + "Vanity url: " + EntityCards [i].vanity_urls[0] + "\r\n";
      }
    }
    

    【讨论】:

      【解决方案2】:

      在处理 Ajax 调用时做你应该做的事情:使用回调。

      例如:

      function geisson(callback) {
          var iabile = new XMLHttpRequest();
          iabile.onreadystatechange = function () {
              if (iabile.readyState == 4) {
                  // ...
                  callback(objectson.cards);
              }
          }
          iabile.open("GET", "gnekcard.json", true);
          iabile.send(null);
      }
      

      在你的第三个功能中:

      function someName() {
          geisson(function(data) {
              for (var i = 0; i < data.length; i++) {
                  //... do something with data ...
              }
          });
          // call Entity the same way here...
      }
      

      顺便说一句,在您的第一个函数中,您总是覆盖 same 元素 (document.getElementById('image')) 的 src 属性。在这里迭代整个数组是没有意义的,最终正弦,src 将具有与最后一个元素相关的值。要么将值分配给多个元素,要么只获取数组中的最后一个元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        相关资源
        最近更新 更多