Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: How to do function.function(param).function?

Thanks for reading.

So I am working on a my first node.js app. I'm relatively familiar with javascript but not well enough.

I have declared a class FOO with a method called bars(index, value} that accepts 2 params. In order do use this, after creating an instance, I have the following fooInstance.bars(3, 2)

I would like to call this method a bit differently. How can I change my FOO definition so that I can use it like this fooInstance.bars(3).value?

My current code is below

var util = require('util'),
  events = require('events');

var FOO = function(opts) {
  this.ipAddress = opts.ipAddress;
  this.port = opts.port;
};

FOO.prototype = new events.EventEmitter;
module.exports = FOO;

FOO.prototype.bars = function (index, value) {
  switch(index) {
    case 1:  
      console.log("Apple " + " at " + value)
      break;
    case 2:
      console.log("Banana, " + " at " + value)
      break;
    case 3: 
      console.log("Cherry, " + " at " + value)
      break;
    case 4:
      console.log("Date, " + " at " + value)
      break;
    default:
      break;
  }
}

thanks in advance!

like image 572
Ervin E Avatar asked Jun 17 '26 22:06

Ervin E


2 Answers

It is called Method Chaining or sometimes Fluent interface. The main idea behind the 'chaining' is to return an object (often times self) as a result, enabling direct invocation on the returned value.

I copied a sample code from here (attribute goes to the original author) that returns self as a return value.

var obj = {
        function1: function () {
            alert("function1");
            return obj;
        },
        function2: function () {
            alert("function2");
            return obj;
        },
        function3: function () {
            alert("function3");
            return obj;
        }
    }


obj.function1().function2().function3();

For your FOO implementation, try returning this at the end of bars function.

FOO.prototype.bars = function(index,value){
  // your previous code here;
  this.value = value;
  return this;
}      
like image 105
Kita Avatar answered Jun 20 '26 10:06

Kita


You are not asking for method chaining. More like

> console.log(fooInstance.bars(3).value)
> Cherry

then do the following:

var util = require('util'),
  events = require('events');

var FOO = function(opts) {
  this.ipAddress = opts.ipAddress;
  this.port = opts.port;
};

FOO.prototype = new events.EventEmitter;
module.exports = FOO;

FOO.prototype.bars = function (index) {
  var undef;
  switch(index) {
    case 1:  
      return { value : 'Apple' };
    case 2:
      return { value : 'Bannana' };
    case 3: 
      return { value : 'Cherry' };
    case 4:
      return { value : 'Date' };
    default:
      return { value : undef };
  }
}   

I'm not exactly sure if you wanted a string back as a value but just guessing. This will return an object as an answer which then can be used like ".value".

What I do to case statements that is simpler is this:

var easierThanCase = {
  '1' : 'Apple',
  '2' : 'Bannana',
  '3' : 'Cherry',
  '4' : 'Date'
};

return { value : easierThanCase[index+''] };
like image 45
King Friday Avatar answered Jun 20 '26 12:06

King Friday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!