【问题标题】:Is eval function necessary here?这里需要 eval 函数吗?
【发布时间】:2021-06-05 13:54:06
【问题描述】:

我想问你,在这段代码中省略 eval () 函数是否合理。特别是如何

  <script>
...
...

        function addGeoJson (geoJsonPath, iconPath = "leaflet-2/images/marker-icon.png", iconSize = [30,50], popUpContent, sideBarContent)
         
        {$.getJSON(geoJsonPath,function(data){
        
          var geoIcon = L.icon({
                        iconUrl: iconPath,
                        iconSize: iconSize
                        });
      
        L.geoJson(data,{
          pointToLayer: function(feature,latlng){
            var marker = L.marker(latlng,{icon: geoIcon, riseOnHover: true});
            
            marker.bindPopup(eval(popUpContent));
            
            marker.on('mouseover',function() {
              document.getElementById('sidebar').innerHTML = eval(sideBarContent);
            });
            
            marker.on('mouseout', function(){
              document.getElementById('sidebar').innerHTML = " ";
            });
            
            return marker;
          }
           
        }).addTo(map);
      });
      };

var layer1pop = "feature.properties.name + '<br/>' + feature.properties.info";
var layer1side = "feature.properties.price + '<br/>' + feature.properties.web";

addGeoJson("points.geojson", undefined, undefined, layer1pop, layer1side);

...
...
</script>

我想重用带有多个 geoJSON 文件的 addGeoJson 函数,并且为每个文件使用不同的标记模板、弹出窗口、侧边栏等。在我不知道 geoJSON 可以有多少属性以及是否我需要更改弹出窗口中属性项的顺序,例如我可以编辑 addGeoJson 基金的参数。如果我试图通过,就会发生错误

var layer1pop = feature.properties.name + '<br/>' + feature.properties.info;
var layer1side = feature.properties.price + '<br/>' + feature.properties.web;

直接因为不可能到达本地feature 并且在传递参数时它甚至不存在。 我尝试用new Function 替换它,但也无法访问feature

你有什么想法吗? 谢谢。

【问题讨论】:

  • 为什么不直接把feature传给addGeoJson(),然后在里面改造呢?
  • 你基本上是在问我们如何处理我们看不到的数据。 eval() 不会创建处理所需的逻辑。也许您有一些类别可以用来帮助构建这种逻辑?
  • @JeremyThille 是的,我可以在 func 中创建模板并根据某些参数切换它们。但是代码越少,func越小,只通过参数调整不是更好吗?
  • @charlietfl eval() 在这种情况下对我来说工作得很好,但我认为由于一些安全风险而省略eval() 的任何使用会更好,而且很多人呼吁放弃eval() func... 是否有可能仅通过调用addGeoJson 以及一些代码来更改这些内容。我可以拥有数十个这样的 geoJson 源,并且每个源都可能有不同的属性,所以我不想为此复制代码这么多次。
  • 我猜如果没有 eval() 也一样。如果没有 geoJSON 对象的实际示例,它就很难说出来

标签: javascript jquery json leaflet eval


【解决方案1】:

为什么不换行marker.bindPopup(eval(popUpContent));marker.bindPopup(feature.properties.name + '&lt;br/&gt;' + feature.properties.info);

并将相同的逻辑应用于第二个函数。
如果有什么东西阻止了你,请重新编写它以使其正常工作。
eval 是一种邪恶的代码气味

如果映射不断变化(即您将使用 feature 的哪一部分),请尝试传递一个知道要提取什么的函数

var layer1pop = function(feature){
 return feature.properties.name + '<br/>' + feature.properties.info;
}

然后marker.bindPopup(eval(popUpContent)); 变成marker.bindPopup(popUpContent(feature));

【讨论】:

  • 完美!非常感谢。传递函数工作正常 :) 我之前尝试过,但现在我想我忘记将 feature 数据传递给 layer1pop func 并且只传递了 marker.bindPopup(popUpContent); ...关于第一个问题,我不想做它导致我必须为每个新的 geoJson 文件(或弹出/侧边栏内容)创建新功能(或复制整个代码块)。
猜你喜欢
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多