代码之家  ›  专栏  ›  技术社区  ›  Saqib Ali

如何重写此Javascript代码以避免JSHint警告?

  •  1
  • Saqib Ali  · 技术社区  · 8 年前

    我是一个新手,正在开发一个使用HTML、JavaScript(Angular 1.5.0)和;CSS。我正在使用Grunt来处理、jslint、缩小我的javascript和CSS文件。我的Javascript代码包括一个由d3.js创建的SVG图像。

    我目前从jshint收到以下警告:

    line 1932  col 32  Don't make functions within a loop.
    

    如何重写下面的代码以消除此警告?

    Line #1924        for (var j = 0; j <= 5; j++) {
    Line #1925          var myVarX = j * 100;
    Line #1926          var myVarY = j * 200;
    Line #1927          var myText = self.svgContainer.append("text")
    Line #1928            .attr("x", myVarX)
    Line #1929            .attr("y", myVarY)
    Line #1930            .text("Hello World")
    Line #1931            .attr("fill", "black")
    Line #1932            .attr("transform", function(d){
    Line #1933                var bb = this.getBBox();
    Line #1934                leftBoundary = bb.x + (bb.width / (-2));
    Line #1935                return "translate(" + (bb.width / (-2)) + ", 0)";
    Line #1936              }
    Line #1937            )
    Line #1938          if (leftBoundary > centerX + 5)
    Line #1939            myText.style("display", "block");
    Line #1940          else
    Line #1941            myText.remove();
    Line #1942        }
    
    4 回复  |  直到 8 年前
        1
  •  4
  •   Stephen Gilboy    8 年前

    移动这个

    function(d){
        var bb = this.getBBox();
        leftBoundary = bb.x + (bb.width / (-2));
        return "translate(" + (bb.width / (-2)) + ", 0)";
    }
    

    在for循环的范围之外。例如:

    var transformCallback = function(d) { 
        var bb = this.getBBox();
        ...
    

    然后使用 transformCallback 代替功能。

    .attr("transform", transformCallback)
    
        2
  •  1
  •   Yordan Ivanov    8 年前
    Function.prototype.getTransform = function() {
      var bb = this.getBBox();
      leftBoundary = bb.x + (bb.width / (-2));
      return "translate(" + (bb.width / (-2)) + ", 0)";
    };
    
    ...
    
    for (var j = 0; j <= 5; j++) {
      var myVarX = j * 100;
      var myVarY = j * 200;
      var myText = self.svgContainer.append("text")
        .attr("x", myVarX)
        .attr("y", myVarY)
        .text("Hello World")
        .attr("fill", "black")
        .attr("transform", this.getTransform())
      if (leftBoundary > centerX + 5)
        myText.style("display", "block");
      else
        myText.remove();
    }
    
        3
  •  1
  •   altocumulus    8 年前

    又一个D3- 伊什 该方法将完全摆脱外部for循环。每当您遇到D3语句的for循环时,这应该引起严重怀疑。为了避免这种情况,您可以使用其强大的数据绑定功能:

    d3.selectAll("text")
      .data(d3.range(6).map(function(d) {         // data binding replaces for-loop
        return {
          x: d * 100,
          y: d * 200,
          remove: false                           // used to mark the texts for removal
        };
      }))                            
      .enter().append("text")
        .text("Hello World")
        .attr("x", function(d) { return d.x; })   // replaces myVarX
        .attr("y", function(d) { return d.y; })   // replaces myVarY
        .attr("fill", "black")
        .attr("transform", function(d){
           var bb = this.getBBox();
           // mark texts for removal based on the condition
           d.remove = (bb.x + (bb.width / (-2))) <= centerX + 5;
           return "translate(" + (bb.width / (-2)) + ", 0)";
        })
        .style("display", "block")
      .filter(function(d) { return d.remove; })   // select all texts marked for removal
        .remove();
    

    这是D3纯粹主义者的做法:都在数据中!该方法使用数据对象保存位置的所有信息 x y 还有一面旗帜 remove 用于指示是否根据某些条件删除文本。除了删除for循环外,这将删除一些其他变量,如 myVarX myVarY ,还将集成块以删除某些元素。

        4
  •  0
  •   Ravi Tiwari    8 年前
    Line 1924 : Change `j++` to `j+=1`
    Line 1932: Take out the function def outside the loop. Define it outside and use it here 
    
    // function defined outside the loop
    function transformFn(d) {
      var bb = this.getBBox(); // if using jQuery replace this with $(this) or that/self to ensure correct this binding
      leftBoundary = bb.x + (bb.width / (-2));
      return "translate(" + (bb.width / (-2)) + ", 0)";
    }
    
    
    // function used at Line1932 
    .attr('transform', transformFn)
    

    休息对我来说很好。