如何使用javascript将参数传递给另一个函数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (66)

以下是我使用的代码:

function renderGrid(element, settings) {
    var x = settings.columns;
    var y = result.length / x;

    $('.grid').remove();
    for (var cols = 0; cols < x; cols++) {
        for (var rows = 0; rows < y; rows++) {
            numberOfTiles = x * y;
            $('#container').append("<div class='grid'><div class = 'usernameSpace'></div></div>");
        };
    };
    $('.grid').width(800 / x);
    $('.grid').height(800 / x);
}

(function ($) {
    $.fn.tiles = function (options) {
     
        var settings = getOptions(options);

        return this.each(function () {

            var grid = renderGrid(this, options);

            fillTilesWithContent(this, options);

            colorTiles(this, options);

            columnCountSet(this,options);
            
            return grid;
        });
    }
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

提问于
用户回答回答于

将相关方法放入类中并设置类变量,例如:

Class GridRenderer {
  var _container;
  function call(element, settings) {
    _container = element;
    renderGrid(settings);
  }
  function createGrid(x, y) {
    $('.grid').remove();
    for (var cols = 0; cols < x; cols++) {
      for (var rows = 0; rows < y; rows++) {
        numberOfTiles = x * y;
        _container.append("<div class='grid'><div class = 'usernameSpace'></div></div>");
      };
    };
    $('.grid').width(800 / x);
    $('.grid').height(800 / x);
  };

  function refreshGrid(x, y) {
    createGrid(x, y);
  };

  function renderGrid(settings) {
    var x = settings.columns;
    var y = result.length / x;
    refreshGrid(x, y);
  }
}

然后

var grid = GridRenderer.call(this, options);

扫码关注云+社区

领取腾讯云代金券