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: 
You can do something like this

```js
const firstFun  = x => x + 1;
const secondFun = x => x + 1;
const thirdFun  = x => x + 1;
const fourthFun = x => x + 1;

const pipe = (...functions) => x => functions.reduce((x, f) => f(x), x);

const initFun = pipe(firstFun, secondFun, thirdFun, fourthFun);

console.log(initFun(3));
```
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;
};

```