-2

我看过代码:

var WAX = function () {
    var _arrInputs;
    window.addEventListener('waxSetArr', function(evt) {
        _arrInputs = evt.detail;
    });
    return {
        getElement: function (i) {
            return _arrInputs[i];
        }
    }
}();
function waxGetElement(i) {
    return WAX.getElement(i);
}

我在我的网站上发现了这段奇怪的 javascript 代码,尽管我不是有意添加它的。我已经在互联网上搜索了它的作用。我仍然无法真正理解它的作用。这段代码有什么作用,有人可以向我解释吗?

4

1 回答 1

1

添加了一些解释代码本身的注释。无法告诉您它在您拥有的页面的上下文中实际做了什么,因为没有足够的信息,但您应该能够弄清楚。

    //Creates a function and assigns it to the variable WAX.  This is then
    //also called at this point.
    var WAX = function () {
      var _arrInputs;

      //Adds an event listener - when 'waxSetArr' is raised
      //this will set the _arrInputs to equal the evt.detail.
      //(Which would give the appearance of being an array).
      window.addEventListener('waxSetArr', function(evt) {
        _arrInputs = evt.detail;
      });

      //Returns a function, that when called will return the item
      //in the array at index i.
      return {
        getElement: function (i) {
            return _arrInputs[i];
        }
     }
   }();

   //Calls the function stored in the variable above - returns the element
   //at index i (if it exists).
   function waxGetElement(i) {
      return WAX.getElement(i);
   }
于 2013-05-11T10:11:23.170 回答