【问题标题】:How to convert from ES6 to ES5(arrow function)如何从 ES6 转换为 ES5(箭头函数)
【发布时间】:2019-10-07 08:48:37
【问题描述】:

我正在尝试将 ES6 箭头函数转换为 ES5

我已经尝试更改它,但它失去了它的范围,因为我正在使用 this.getView()

this.getModel().read('/CharacteristicSet', {
  filters: this._afilters,
  success: function (oData) {
    oViewModel.setProperty('/charSet', oData.results);

    for (let i = 0; i < oData.results.length; i++) {
      if (oData.results[i].GroupId === sKey) {
        oBinding.filter(this._mFilters[sKey]);
      }
    }

    let aIconTabItems = this.byId('iconTabBar').getItems();
    let aCharacteristics = oData.results;

    for (var j = 0; j < aIconTabItems.length; j++) {
      let count = aCharacteristics.filter(
        obj =>
          obj.GroupId === aIconTabItems[j].getKey() &&
          obj.EquipmentNumber ===
            this.getView()
              .getBindingContext()
              .getProperty('EquipmentNumber'),
      ).length;

      oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
    }
  }.bind(this),
});

我希望它是 ES5

【问题讨论】:

    标签: javascript ecmascript-6 sapui5 ecmascript-5


    【解决方案1】:

    一种可能的解决方案是使用Array.filter()thisArg

    thisArg:可选 - 执行回调时用作 this 的值。

    特别是,谈到您尝试转换的箭头函数,您可以将success 回调中的this 上下文作为this 用于filter() 内部:

    var count = aCharacteristics.filter(function(obj)
    {
        var equipmentNum = this.getView().getBindingContext().getProperty("EquipmentNumber");
        return obj.GroupId === aIconTabItems[j].getKey() && obj.EquipmentNumber === equipmentNum;
    }, this /* Here we use the thisArg of filter */).length;
    

    另一种方法,就是在loop 之外定义一个变量,就像你对变量aIconTabItems 所做的那样:

    let aIconTabItems = this.byId('iconTabBar').getItems();
    let aCharacteristics = oData.results;
    let equipmentNum = this.getView().getBindingContext().getProperty('EquipmentNumber');
    
    for (var j = 0; j < aIconTabItems.length; j++)
    {
        let count = aCharacteristics.filter(function(obj)
        {
            return obj.GroupId === aIconTabItems[j].getKey() &&
                   obj.EquipmentNumber === equipmentNum;
        }).length;
    
        oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      相关资源
      最近更新 更多