簡體   English   中英

如何在Javascript函數中調用函數

[英]How to invoke a function within a function in 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 () {}, dataset2

該對象的返回值是一個對象,但是您根本沒有為其提供data屬性。 (編寫Dataset函數的人確實為它的返回值提供了data屬性)。

您需要定義您要調用的函數。

我想你看起來像這樣

      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'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM