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

时间:2016-04-27 05:19:40

标签: javascript angularjs d3.js jshint grunt-contrib-jshint

我是一个新手开发一个使用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 个答案:

答案 0 :(得分:4)

移动此

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)

答案 1 :(得分:1)

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();
}

答案 2 :(得分:1)

另一个D3- ish 方法将完全摆脱外部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纯粹主义者所采取的方式:数据全部都在!该方法使用数据对象来保存位置xy的所有信息以及标记remove,用于指示是否基于某些条件移除文本。除了去除for循环之外,这将除去一些其他变量,如myVarXmyVarY,并且还将整合块以删除一些元素。

答案 3 :(得分:0)

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)

休息对我来说很好。