谁能看到丢失的花括号

问题描述 投票:0回答:3

我不知所措。我已经花了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,{但是tbh我认为我的大脑在这一点上已经挡住了花括号的视线。请帮助?

javascript syntax-error curly-braces
3个回答
0
投票

大括号表示新作用域,或者是对象常量,因为这是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()中-应该吗?如果您可以回答这些问题,则放置花括号是一项非常简单的任务,但如果您不能这样做,我们也不能(因此也无法为您修复代码)。


0
投票

正如@ 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);
  });
}


-1
投票
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)
});}
© www.soinside.com 2019 - 2024. All rights reserved.