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?

6
  • 2
    which function are you trying to invoke, there is nothing called data Commented Jan 5, 2016 at 7:56
  • @ArunPJohny: Anon function that return nof_genes.This post is an attempt to solve this related post: stackoverflow.com/questions/34588069/… Commented Jan 5, 2016 at 7:59
  • Perhaps you want use it this way: dataset2: {data: new function () {...}} Commented Jan 5, 2016 at 7:59
  • @ArunPJohny I really appreciate if that post you can help? Commented Jan 5, 2016 at 8:01
  • I think your error should looks like 'this.dataset2.data is not a function' Commented Jan 5, 2016 at 9:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

would you mind looking at this related post: stackoverflow.com/questions/34588069/…
How do you create data property in a function? I tried data2: new function () {retire data.nof_genes};
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).
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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.