如何在Javascript中调用函数中的函数
我有以下功能如何在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.datase
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"
}]
}]
}];
我的问题是我想创建类似的函数。而不是
这是基于
// 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
正确的方法是什么?在第一个示例中:
dataset
的值为new Plottable.dataset(),
它的返回值是一个具有data
函数的对象,因此可以将data
作为函数调用
在第二个例子中:
dataset2
的值是newfunction(){},
它的返回值是一个对象,但您根本没有给它一个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'));
您试图调用哪个函数,没有任何函数被称为
data
@ArunPJohny:Anon函数返回nof_genes
。此帖子试图解决此相关帖子:也许您希望这样使用:dataset2:{data:new function(){…}
}@ArunPJohny我真的很感激你能帮上忙吗?我想你的错误应该是“this.dataset2.data不是函数”你介意看看这篇相关的帖子吗:你如何在函数中创建数据属性?我尝试了data2:newfunction(){retiredata.nof_genes}代码>在事实之后:ref\u to\u object.data=something
。提前把东西放在原型链上。(这意味着学习JavaScript原型继承,我不会在评论中解释这一点)。
this.dataset2.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'));