3

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!

Ervin E
  • 442
  • 5
  • 18

4 Answers4

5

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;
}      
Kita
  • 2,604
  • 19
  • 25
2

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+''] };
King Friday
  • 25,132
  • 12
  • 90
  • 84
1

You have two possibilities:

  1. You can simply pass the arguments you need. Javascript will set arguments not used to undefined. Then, in the function, you can check by if (!value) or if (typedef value === "undefined") to find the state. (Javascript is in general very flexible here. You can also get arguments you pass but you didn't declare in the function definition).

  2. You can create a new function bars() even though the name has already been used. But doing so will destroy the old function.

In order to check 1, try this:

var test = new FOO();
console.log(test.bars(3));

It'll anwer: Cherry, at undefined

In order to check 2, add after the definition of bars:

FOO.prototype.bars = function(index) {
    console.log("In new bars!");
}

Here are more infos, also about the keyword arguments: How to get function parameter names/values dynamically from javascript

Community
  • 1
  • 1
peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
  • 1
    As the question is a bit unclear, I decided to undelete this post. Many people may see different aspects in the question. While some explain about method chaining, others suggest to alter the `switch` or else. In my answer, I emphasize the flexibility of Javascript handling function arguments. May it be of use for sb. – peter_the_oak Jul 04 '14 at 08:15
0

Here is a better way to implement your bars method:

FOO.prototype.bars = function (index, value) {
    var fruitArray = ["Apple", "Banana", "Cherry", "Data"];
    console.log(fruitArray[index - 1] + " at " + value);
}

If you are wanting to do this: fooInstance.bars(3).value. You are calling bars with one parameter (index === 3) and then calling the property of value on this result. This logic does not make much sense in this example. Hope this helps.