对这个es6承诺和箭头功能感到困惑

时间:2017-05-18 11:16:41

标签: javascript ecmascript-6 promise

在这个要点的app.js中:https://gist.run/?id=acf8253329939b2e046cd0e3394351fe SuggestionService如何运作?

export class SuggestionService {
  constructor(model, countries) {
    this.model = model;
    this.countryIndex = countries;
    this.countries = Object.keys(countries);
  }

  country = {
    suggest: value => {
      if (value === '') {
        return Promise.resolve([]);
      }
      value = value.toLowerCase();
      const suggestions = this.countries
        .filter(x => x.toLowerCase().indexOf(value) === 0)
        .sort();
      return Promise.resolve(suggestions);
    },

    getName: suggestion => suggestion
  };

  city = {
    suggest: value => {
      if (value === '' || this.model.country === null) {
        return Promise.resolve([]);
      }
      value = value.toLowerCase();
      let suggestions = this.countryIndex[this.model.country]
        .filter(x => x.toLowerCase().indexOf(value) === 0);
      suggestions = suggestions.filter((x, i) => suggestions.indexOf(x) === i)
        .sort();
      return Promise.resolve(suggestions);
    },

    getName: suggestion => suggestion
  };
}

我不明白行getName: suggestion => suggestion,我认为它是从建议承诺中选择元素,这是正确的吗?这是如何工作的?

app.html中,此suggestion属性用于绑定:

<span style="font-style: italic">${suggestion}</span>

0 个答案:

没有答案