0

I have the following function

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]);
      }
    };
  });

And the it is invoked like this

 processData(input_data);

with the following 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"
    }]
  }]
}];

My question is I want to create the similar function. But instead of this Plottable based

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

I want to create the anonymous function something like this:

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

But when I tried that I get this message:

this.dataset2.data is not a function

What's the right way to do it?

neversaint
  • 60,904
  • 137
  • 310
  • 477

2 Answers2

2

In the first example:

this.dataset.data

The value of dataset is new Plottable.Dataset(),.

The return value of that is an object which has a data function, so you can call data as a function.

In the second example:

this.dataset2.data

The value of dataset2 is new function () {},.

The return value of that is an object, but you haven't given it a data property at all. (The people who wrote the Dataset function did give its return value a data property).

You need to define the function you are trying to call.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • would you mind looking at this related post: http://stackoverflow.com/questions/34588069/how-to-make-label-in-histogram-respond-to-dynamic-user-input – neversaint Jan 05 '16 at 08:02
  • How do you create `data` property in a function? I tried `data2: new function () {retire data.nof_genes};` – neversaint Jan 05 '16 at 08:11
  • After the fact: `ref_to_object.data = something`. In advance? Put something on the prototype chain. (Which means learning JavaScript prototypal inheritance, which isn't something I'm going to try to explain in a comment). – Quentin Jan 05 '16 at 09:58
0

I think you are looking something like this,

      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'));
Bharat DEVre
  • 539
  • 3
  • 13