簡體   English   中英

為什么這個函數只在 IE11 上需要分號?

[英]Why does this function expect a semicolon only on IE11?

我正在編寫一些與 IE 11 兼容的 vuejs 代碼。 我一直看到這個函數出現預期的分號錯誤:

chemicalFilters: function (chemical) 
{            
  var max = 0;
  var min = 100;
  for (var component of this.components)
  {
     if(component.component_name == chemical)
     {
       if (max < component.component_value)
         max = component.component_value;
       if(component.component_value != 0 && min > component.component_value)
         min = component.component_value;
     }
  }

  if(max == 0 && min == 100)
    min = max;
  else
  {
    min = Math.round(min*100);
    max = Math.round(max*100);
  }

  this.component_filters.push({component_name: chemical, range:[min,max], min:min, max:max, originalRange:[min,max]});
},

特別是這一行:

if(component.component_name == chemical)

問題不在你引用的那一行,而是它上面的那一行。 for...of構造是在 ECMAScript 2015 中引入的,IE11 並不完全支持它。 MDN

瀏覽器兼容性

您可以使用Babel 之類的工具將其轉換為向后兼容的代碼,因此:

for (var component of this.components) {
  //...
}

將被轉換為這樣的東西(取決於您的設置):

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
  for (var _iterator = this.components[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
    //...

    var component = _step.value;
  }
} catch (err) {
  _didIteratorError = true;
  _iteratorError = err;
} finally {
  try {
    if (!_iteratorNormalCompletion && _iterator.return != null) {
      _iterator.return();
    }
  } finally {
    if (_didIteratorError) {
      throw _iteratorError;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM