如何在Javascript中调用函数内的函数

时间:2016-01-05 07:52:01

标签: 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

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

在第一个例子中:

this.dataset.data

dataset的值为new Plottable.Dataset(),

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

在第二个例子中:

this.dataset2.data

dataset2的值为new function () {},

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

您需要定义您要调用的功能。

答案 1 :(得分:0)

我认为你看起来像这样,

      function dataset (){
         this.data=function(ip){
             //process your data here
             return ip;
          }
       }

      var dataset=new dataset();
      console.log(this.dataset.data('This is input data'));