函数内的访问器功能

问题描述 投票:-1回答:1

你能在一个函数中放一个访问函数吗?例如,像这样:

    function filterByCounty(data, county) {
    xValue: function (d){ return d.element; },
    yValue: function (d){ return d.value; }
    return data.filter(function (d){
      return d.County === county;
    });
  }
javascript function d3.js
1个回答
-1
投票

您可以在xValueyValue上定义访问器,如下所示:

function filterByCounty(data, county) {
  // `this` refers to the function
  Object.defineProperties(this, {
    xValue: {
      get: () => {
        return d => d.element;
      }
    },
    yValue: {
      get: () => {
        return d => d.value;
      }
    }
  });
  return data.filter(d => {
    return d.County === county;
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.