Question: I have a function like that:

```
function intiFun(initValue) {

    const firstResult = firstFun(initValue);
    const secondResult = secondFun(firstResult);
    const thirdResult = thirddFun(secondResult);
    const fourthResult = fourthFun(thirdResult);
    return fourthResult;
}

```

but i want to write it better. and i dont want to save value from each function as variable.

is there any solution to to call functions with out save old value

like rxjs or somthing like that: 

```
function intiFun(initValue) {
    return firstFun(initValue).secondFun().thirddFun().fourthFun();
}

```

or more better like that:

```
function intiFun(initValue) {
    return firstFun(initValue)         
          .secondFun(secondInput)
          .thirddFun(secondInput)
          .fourthFun(secondInput)
}

function secondFun(value, secondInput) {
    return ...;
}
...

```

or some liberally to do that (maybe lodash)

Answer: 
The result you're looking for can be achieved using [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce).

```js
let log = (head, ...args) => { console.log('log:', head, ...args); return head },
    firstFun  = (str, ...args) => log(str, ...args) + ' firstFun',
    secondFun = (str, ...args) => log(str, ...args) + ' secondFun',
    thirddFun = (str, ...args) => log(str, ...args) + ' thirddFun',
    fourthFun = (str, ...args) => log(str, ...args) + ' fourthFun';

function initFun(initValue) {
  let functions = [
    [firstFun],
    [secondFun, 'extra argument'],
    [thirddFun],
    [fourthFun, "I'm here too"],
  ];

  return functions.reduce((result, [fn, ...args]) => fn(result, ...args), initValue);
}

console.log( 'result: ' + initFun('foo bar') );
```

Keep in mind that I log the incomming arguments of the methods, not the resulting value. This means that for example `secondFun` (`log: foo bar firstFun extra argument`) has the argument `'foo bar firstFun'` and `'extra argument'`. But you only see the added string `'secondFun'` when `thirdFun` is called (since it is given as the argument).
Bad Method — See Below
----------------------

~~If you want something like `a.firstFunc().secondFunc().thirdFunc().fourthFunc()`, you should define those functions to `Object.prototype` (or `Number.prototype`, `String.prototype`, etc.):~~

```
Object.prototype.firstFunc = function() {
    var value = this;
    // ...
    return something;
};
Object.prototype.secondFunc = function() {
    var value = this;
    // ...
    return something;
};
Object.prototype.thirdFunc = function() {
    var value = this;
    // ...
    return something;
};
Object.prototype.fourthFunc = function() {
    var value = this;
    // ...
    return something;
};

```

***P.S.*** "Function" is normally shortened to "func" but not "fun".

---

Update
------

If you want something like `myObject(a).firstFunc().secondFunc().thirdFunc().fourthFunc()`, you should:

```
var myObject = function(value) {
    this.value = value;
};
myObject.prototype.firstFunc = function() {
    var value = this.value;
    // ...
    return something;
};
myObject.prototype.secondFunc = function() {
    var value = this.value;
    // ...
    return something;
};
myObject.prototype.thirdFunc = function() {
    var value = this.value;
    // ...
    return something;
};
myObject.prototype.fourthFunc = function() {
    var value = this.value;
    // ...
    return something;
};

```