【问题标题】:Can anyone spot the missing curly bracket谁能发现丢失的大括号
【发布时间】:2019-12-17 17:31:21
【问题描述】:

我束手无策。我一直在修改我的代码 3 个小时,现在试图解决这个丢失的大括号。每次我认为我已经修复它时,它都会在不同的行上出现一个新的缺失花括号。请帮帮我,我想哭。

代码如下:

 function addCTHValue() {

    $.getJSON("https://api.myjson.com/bins/nh71g")  
    .done(function(data) {
    });

        function processCTHValueData(data) {

            var min = Infinity; 
            var max = -Infinity; 


            for (var feature in data.features) {
                var properties = data.features[feature].properties;

            for (var attribute in properties) {
                  if ( attribute = 'CTH Value' )

                  {
                      if (properties[attribute] < min) {
                          min = properties[attribute]; 
                      }
                      if (properties[attribute] > max) {
                          max = properties[attribute]; 
                      }
                  }
            }}  

          return { 
              min : min,
              max : max
        }}


    function CTHValueSymbols(data) {


        CTHValueCountries = L.geoJson(data, {


            pointToLayer: function(feature, latlng) {
                return L.circleMarker(latlng, { 
                    fillColor: "#501e65",  
                    weight: 2,            
                    fillOpacity: 0.5,      
                    radius: feature.properties["CTH Value"]/100
                })}
                .on({

                        mouseover: function(e) {
                            this.openPopup();
                            this.setStyle({fillColor: 'green'});  
                        },
                        mouseout: function(e) {
                            this.closePopup();
                            this.setStyle({fillColor: '#501e65'});
                        }
                });
        }
        }).addTo(map);

    function calcCTHValueRadius(attributeValue) {

        var scaleFactor = 0.01;
        var area = attributeValue * scaleFactor;

        return Math.sqrt(area/Math.PI);
        }

        $.getJSON("https://api.myjson.com/bins/nh71g").done(function(data) {

        var info = processCTHValueData(data);
        CTHValueSymbols(data)
            });
    }

据称缺少的大括号位于以下行:CTHValueCountries = L.geoJson(data, { 但是我认为我的大脑此时已经挡住了大括号的视线。请帮忙?

【问题讨论】:

  • .done() 函数正在立即关闭? .done(function(data) { });
  • processCTHValueData 仅在 addCTHValue() 中定义,您稍后会尝试调用它。我认为人们对范围和括号的用途缺乏了解
  • if ( attribute = 'CTH Value' ) 应该是 if ( attribute == 'CTH Value' )(如果你想要严格相等,则应该是 ===
  • 没有必要粗鲁 Joel M 我是 Javascript 的初学者,我正在尝试学习教程
  • lol 对不起,我以为是别人写的。我不是想无礼。

标签: javascript syntax-error curly-braces


【解决方案1】:

花括号对表示新的作用域,或对象字面量,因为这是 JavaScript。

看看这段代码:

 function addCTHValue() {

    $.getJSON("https://api.myjson.com/bins/nh71g")  
    .done(function(data) {
    });

        function processCTHValueData(data) {

            var min = Infinity; 
            var max = -Infinity; 


            for (var feature in data.features) {
                var properties = data.features[feature].properties;

            for (var attribute in properties) {
                  if ( attribute = 'CTH Value' )

                  {
                      if (properties[attribute] < min) {
                          min = properties[attribute]; 
                      }
                      if (properties[attribute] > max) {
                          max = properties[attribute]; 
                      }
                  }
            }}  

          return { 
              min : min,
              max : max
        }}

你能知道addCTHValue() 运行时执行了哪些代码行吗? 应该执行哪些行?如果你能回答这个问题,放置大括号就变得非常容易。这段代码有点乱,所以我只能尝试回答这个问题。

一对大括号之间的每一行代码(即在代码块或对象字面量内)应该比周围的代码多一个缩进级别。此外,在一行上写两个花括号是相当糟糕的风格,所以不要这样做,特别是如果你不理解它们的目的。考虑到这些,让我们重写上面的代码:

 function addCTHValue() {

    $.getJSON("https://api.myjson.com/bins/nh71g")  
    .done(function(data) {
    });

    function processCTHValueData(data) {

       var min = Infinity; 
       var max = -Infinity; 


       for (var feature in data.features) {
          var properties = data.features[feature].properties;

          for (var attribute in properties) {
             if ( attribute = 'CTH Value' ) {
                if (properties[attribute] < min) {
                   min = properties[attribute]; 
                }
                if (properties[attribute] > max) {
                   max = properties[attribute]; 
                }
             }
          }
       }  

       return { 
          min : min,
          max : max
       }
    }

注意:在此代码块中,从function addCTHValue() 直接向下不要到达花括号。函数在哪里结束?显然processCTHValueData(data) 现在实际上包含在addCTHValue() 中——它应该是吗?如果你能回答这些问题,放置花括号是一项非常简单的任务,但如果你不能,我们也不能(因此无法为你修复代码)。

【讨论】:

  • 我不知道 processCTHValueData(data) 是否应该进入 addCTHValue()。我以为是。我正在尝试遵循本教程:neiugis.github.io/lab7 但我真的很挣扎。即使复制了其他人的解决方案,我仍然收到一条错误消息,提示我缺少一个大括号。
  • @sophhGIS 看那个教程,我可以看到直接复制了哪些部分,但不知何故你的复制不完整......就像 function CTHValueSymbols() 被复制时没有最后一行和大括号,出于某种原因,我什至根本没有看到$.getJSON("https://api.myjson.com/bins/nh71g") 在任何功能中。我认为您需要遵循基本的 javascript 教程并尝试真正理解代码在做什么,然后再尝试简单地按顺序复制每个代码块并使其工作。
  • 我没有全部复制,因为我不想使用时间滑块。我找不到不使用插件创建地图的基本比例圆地图。
【解决方案2】:

正如@joel-m 所说,代码是一团糟,看起来你以前甚至没有测试过,据我所知有很多错误,但看起来你总是把{} 放在一边。

addTo(map) 不在GeoJS 调用的调用范围内。现在这应该可以工作了

function addCTHValue() {
  $.getJSON('https://api.myjson.com/bins/nh71g').done(function(data) {});

  function processCTHValueData(data) {
    var min = Infinity;
    var max = -Infinity;

    for (var feature in data.features) {
      var properties = data.features[feature].properties;

      for (var attribute in properties) {
        if ((attribute = 'CTH Value')) {
          if (properties[attribute] < min) {
            min = properties[attribute];
          }
          if (properties[attribute] > max) {
            max = properties[attribute];
          }
        }
      }
    }

    return {
      min: min,
      max: max
    };
  }

  function CTHValueSymbols(data) {
    CTHValueCountries = L.geoJson(data, {
      pointToLayer: function(feature, latlng) {
        return L.circleMarker(latlng, {
          fillColor: '#501e65',
          weight: 2,
          fillOpacity: 0.5,
          radius: feature.properties['CTH Value'] / 100
        });
      }.on({
        mouseover: function(e) {
          this.openPopup();
          this.setStyle({ fillColor: 'green' });
        },
        mouseout: function(e) {
          this.closePopup();
          this.setStyle({ fillColor: '#501e65' });
        }
      })
    }).addTo(map);
  }  

  function calcCTHValueRadius(attributeValue) {
    var scaleFactor = 0.01;
    var area = attributeValue * scaleFactor;

    return Math.sqrt(area / Math.PI);
  }

  $.getJSON('https://api.myjson.com/bins/nh71g').done(function(data) {
    var info = processCTHValueData(data);
    CTHValueSymbols(data);
  });
}

【讨论】:

  • 感谢您的帮助我是 Javascript 的超级新手,我正在学习一个教程,但我想我做得很糟糕。如何测试代码?我不太了解如何与 Web 控制台进行通信,所以也许这就是我苦苦挣扎的原因。
  • 我刚刚得到一个新错误:“(中间值).on 不是函数”,你知道这是什么意思吗?
【解决方案3】:
function addCTHValue() {

$.getJSON("https://api.myjson.com/bins/nh71g")
    .done(function(data) {});

function processCTHValueData(data) {

    var min = Infinity;
    var max = -Infinity;


    for (var feature in data.features) {
        var properties = data.features[feature].properties;

        for (var attribute in properties) {
            if (attribute = 'CTH Value')

            {
                if (properties[attribute] < min) {
                    min = properties[attribute];
                }
                if (properties[attribute] > max) {
                    max = properties[attribute];
                }
            }
        }
    }

    return {
        min: min,
        max: max
    }
}


function CTHValueSymbols(data) {
    CTHValueCountries = L.geoJson(data, {
            pointToLayer: function(feature, latlng) {
                            return L.circleMarker(latlng, {
                                fillColor: "#501e65",
                                weight: 2,
                                fillOpacity: 0.5,
                                radius: feature.properties["CTH Value"] / 100
                            })
                            .on({
                              mouseover: function(e) {
                                  this.openPopup();
                                  this.setStyle({fillColor: 'green'});  
                              },
                              mouseout: function(e) {
                                  this.closePopup();
                                  this.setStyle({fillColor: '#501e65'});
                              }
                            })
                        }
    }).addTo(map);
}

function calcCTHValueRadius(attributeValue) {

    var scaleFactor = 0.01;
    var area = attributeValue * scaleFactor;

    return Math.sqrt(area / Math.PI);
}

$.getJSON("https://api.myjson.com/bins/nh71g").done(function(data) {

    var info = processCTHValueData(data);
    CTHValueSymbols(data)
});}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2013-05-10
    • 2019-12-31
    • 2018-06-02
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多