Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain custom javascript functions

After searching for quite some time, I still haven't found what I'm looking for.

There's a fair amount of examples that either require creating a new instance, or only have functions that don't return anything (which means the problem can be solved with returning this).

I hope the following example illustrates my point well:

// Say I have these functions
function aNumber(){
    var max = 100, min = 0;
    return (Math.floor(Math.random() * (max - min + 1)) + min);
}
function divideBy(_number, _divider){
    return (_number / _divider);
}
function multiplyBy(_number, _multi){
    return (_number * _multi);
}
function add(_number, _add){
    return (_number + _add);
}
function subtract(_number, _sub){
    return (_number - _sub);
}

// #########################################################

// I can do this with them
var test = aNumber();
test = divideBy(aNumber, 2);
test = add(aNumber, 5);
test = multiplyBy(aNumber, 3);
test = subtract(aNumber, 10);

// I would like to do this however:
var test = aNumber().divideBy(2).add(5).multiplyBy(3).subtract(10);

What would be the most efficient way to make the last line work?

Am I misinformed that this is possible without creating a new instance of something?

like image 595
KJdev Avatar asked Jan 27 '16 00:01

KJdev


Video Answer


2 Answers

Yes, this requires changing the Prototype of an Object. Objects are instances. So you need to create an object to do this kind of thing.

function MyNum(value) {
  this._val = value;      // Having _variable is for denoting it is a private variable.
}

Initialize objects using:

var myNum = new MyNum(5);

And now using this, define these:

MyNum.prototype.divideBy = function () {}
MyNum.prototype.multiplyBy = function () {}

Don't forget to use return this; inside these functions.

like image 151
Praveen Kumar Purushothaman Avatar answered Oct 06 '22 14:10

Praveen Kumar Purushothaman


Try like below for creating without instance and prototype keyword.

One more method is been added here you can set number or random number by default. if the number not specified.

  var Calculator = {

setNumber: function(givenNumber) {
  var max = 100,
      min = 0;

  this.number = (givenNumber) ? givenNumber : (Math.floor(Math.random() * (max - min + 1)) + min);
  return this;
},

divideBy: function(_divider) {
  this.number = (this.number / _divider);
  return this;
},

multiplyBy: function(_multi) {
  this.number = (this.number * _multi);
  return this;
},

add: function(_add) {
  this.number = (this.number + _add);
  return this;
},

subtract: function(_sub) {
  this.number = (this.number - _sub);
  return this;
},

result: function () {
  return this.number;
}
  }

  document.write('<pre>');
  document.writeln(Calculator.setNumber(2).divideBy(2).add(5).multiplyBy(3).subtract(10).result());
  document.writeln(Calculator.setNumber(4).divideBy(2).add(5).multiplyBy(3).subtract(10).number);
  document.writeln(Calculator.setNumber().divideBy(2).add(5).multiplyBy(3).subtract(10).result());
  document.write('</pre>');
like image 35
Venkat.R Avatar answered Oct 06 '22 13:10

Venkat.R