javascript - 如何在 JavaScript 中调用函数内的函数

标签 javascript

我有以下功能

function processData(data) {
  histograms = data[0].histograms.map(function(data) {
    return {
      title: data.sample,
      dataset: new Plottable.Dataset(),
      dataByThreshold: {},
      load: function(threshold) {
        this.dataset.data(this.dataByThreshold[threshold]);
      }
    };
  });

它是这样调用的

 processData(input_data);

具有以下数据:

var input_data = [{
  "threshold": 1.5,
  "histograms": [{
    "sample": "Sample1",
    "nof_genes": 26,
    "values": [{
      "score": 6.7530200000000002,
      "celltype": "Bcells"
    }, {
      "score": 11.432763461538459,
      "celltype": "DendriticCells"
    }, {
      "score": 25.823089615384621,
      "celltype": "Macrophages"
    }, {
      "score": 9.9911211538461551,
      "celltype": "gdTCells"
    }, {
      "score": 7.817228076923076,
      "celltype": "StemCells"
    }, {
      "score": 17.482806923076922,
      "celltype": "StromalCells"
    }, {
      "score": 29.335427692307697,
      "celltype": "Monocytes"
    }, {
      "score": 28.914959615384621,
      "celltype": "Neutrophils"
    }, {
      "score": 13.818888461538467,
      "celltype": "NKCells"
    }, {
      "score": 9.5030688461538464,
      "celltype": "abTcells"
    }]
  }]
}, {
  "threshold": 2,
  "histograms": [{
    "sample": "Sample1",
    "nof_genes": 30,
    "values": [{
      "score": 5.1335499999999996,
      "celltype": "Bcells"
    }, {
      "score": 16.076072499999999,
      "celltype": "DendriticCells"
    }, {
      "score": 46.182032499999998,
      "celltype": "Macrophages"
    }, {
      "score": 6.5895700000000001,
      "celltype": "gdTCells"
    }, {
      "score": 5.3218800000000002,
      "celltype": "StemCells"
    }, {
      "score": 53.643625,
      "celltype": "StromalCells"
    }, {
      "score": 85.1618225,
      "celltype": "Monocytes"
    }, {
      "score": 55.559129999999996,
      "celltype": "Neutrophils"
    }, {
      "score": 7.6717524999999984,
      "celltype": "NKCells"
    }, {
      "score": 6.3277800000000006,
      "celltype": "abTcells"
    }]
  }]
}];

我的问题是我想创建类似的功能。但不是 这个 Plottable 基于

// dataset: new Plottable.Dataset(),
// this.dataset.data(this.dataByThreshold[threshold]);

我想创建这样的匿名函数:

dataset2: new function () {},
load2: function(threshold) {
   this.dataset2.data(this.dataByThreshold[threshold]);
}

但是当我尝试时,我收到以下消息:

this.dataset2.data is not a function

正确的做法是什么?

最佳答案

在第一个示例中:

this.dataset.data

dataset 的值为 new Plottable.Dataset(),

它的返回值是一个具有data函数的对象,因此您可以将data作为函数调用。

在第二个示例中:

this.dataset2.data

dataset2 的值为 new function () {},

它的返回值是一个对象,但您根本没有给它一个 data 属性。 (编写 Dataset 函数的人确实为其返回值赋予了 data 属性)。

您需要定义您要调用的函数。

关于javascript - 如何在 JavaScript 中调用函数内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606626/

相关文章:

javascript - 在我选择一个值后,更改问题仍然存在

javascript - 变量在代码外部不存在

javascript - 气泡图动态更改 JSON 响应中的 X 标签值

php - Session_id 在页面源代码中可见,可以吗?

javascript - 使用 webpack 编译时无法刷新生产中的页面

javascript - 用于在一张纸上对多个表格进行实时排序的 Google Sheets 脚本

javascript - FilmRoll 加载额外的 <div>

javascript - Angular 服务中的业务规则?

javascript - async.waterfall 失去了范围

javascript - 在 CSS3 或 HTML5 中更改元素背景颜色的有效方法