2016-01-05 23 views
0
函數中的一個函數

我有以下功能如何調用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

功能,你想調用哪個,沒有什麼所謂的'data' –

+0

@ArunPJohny:匿名函數返回'nof_genes'.This後是試圖解決這個相關的帖子:http://stackoverflow.com/questions/34588069/how-to-make-label-in-histogram-respond-to-dynamic-user-input – neversaint

+0

也許你想要這樣使用它:' dataset2:{data:new function(){...}'} –

回答

2

在第一個例子:

this.dataset.data 

dataset值是new Plottable.Dataset(),

該函數的返回值是一個具有data函數的對象,因此您可以調用data作爲函數。

在第二個例子:

this.dataset2.data 

dataset2值是new function() {},

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

您需要定義您嘗試呼叫的功能。

+0

你會介意看看這個相關的帖子:http:// stackoverflow。com/questions/34588069 /如何在標籤中製作標籤 - 響應動態用戶輸入 – neversaint

+0

如何在函數中創建'data'屬性?我試過'data2:new function(){retire data.nof_genes};' – neversaint

+0

事實:'ref_to_object.data = something'。提前?把一些東西放在原型鏈上。 (這意味着學習JavaScript原型繼承,這不是我要在評論中試圖解釋的東西)。 – Quentin

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