-1

For example in the promises:

New Promise(function(){}).then(function(){});

Where is the important thing? , in the syntax of .then

, so how i can create a function that can be applied in other function with "." (dot operator)

per example(pseudocode) :

function doSomething(a,b) {return a+b}.myOwnFunction(function(){
  console.log("Function applied in another function with DOT operator");
});
dhilt
  • 18,707
  • 8
  • 70
  • 85
ESCM
  • 269
  • 2
  • 13

2 Answers2

2

In your example the result of doSomething() call must be an object with myOwnFunction property of function type. It is possible for example via the next approach:

function Wrapper(value) {
  this.value = value;
}
Wrapper.prototype.myOwnFunction = function() {
  console.log("The caller value is " + this.value);
};

And then

function doSomething(a, b) { return new Wrapper(a + b) };
doSomething(1, 2).myOwnFunction(); // The caller value is 3

or

function doSomething(a, b) { return a + b; }
new Wrapper(doSomething(1, 2)).myOwnFunction(); //The caller value is 3
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • I think it would be more in accordance with the question if `Wrapper` took a function - not a value - (i.e. `function Wrapper (f) { this.f = f; } Wrapper.prototype.myOwnFunction = function () { console.log("The function returns " + this.f(); };`) and you wrote `var doSomething = function (a, b) { return a + b; }` followed by `new Wrapper(doSomething).myOwnFunction()`. – jcarpenter2 Nov 02 '17 at 01:54
  • @jcarpenter2 But still where should I pass real values for a, b params? I see only this way with your approach: `new Wrapper(function() { return doSomething(1, 2); }).myOwnFunction();` – dhilt Nov 02 '17 at 02:20
  • Oh yeah, of course. They would either have to be made up inside `myOwnFunction` or passed as parameters to it. – jcarpenter2 Nov 02 '17 at 02:21
  • Also, `myOwnFunction` could be written as a top-level function like so: `var myOwnFunction = function (doSomething, f2) { doSomething(1, 2); f2(); /* etc */ }` and never return an object or use dot notation. It is hard to say, without knowing what the OP wants `myOwnFunction` to do. – jcarpenter2 Nov 02 '17 at 02:23
1

What you are looking for is something called "chainable functions".

Example 1

Consider the example below:

var array = [1, 2, 3];
var index = array.filter(cut).indexOf(3);

console.log(index);

function cut(n){
    return n > 1;
}

The variable index returns the result of the function array.filter(...).indexOf(...).

But how does it work? Let's break it down.

1. Initializing an array

var array = [1, 2, 3];
// A variable `array`is created with the value of an array `[1, 2, 3]`.

2. Using Array method filter

array.filter(cut)
// This function returns an array of value [2, 3];

3. Chaining Array method indexOf to filter

array.filter(cut).indexOf(3);
// equals to [2, 3].indexOf(3)
// returns the value 1.

So why can we chain the function indexOf to filter?

Because array.filter() returns an array, and indexOf and filter are methods that are under the same prototype from Array constructor.

Example 2

Similarly, this example using String constructor will work:

var string = 'This is a string';
var newString = string.substring(0, 4).toUpperCase();

console.log(newString);

Because string.substring(...) returns a string and toUpperCase() is a method from String constructor.

Example 3

This is a little bit trickier.

var string = 'This is a string';
var newString = string.split(' ').indexOf('is');

console.log(newString);

Now, split() is a String method but indexOf() is an Array method. Then why does this work?

Because split() method returns an array value. So, you need a method from Array constructor to chain it.

How to create a chainable function?

You can create your own constructor function with its own prototype.

var a = undefined;

// The constructor
var Example = function(string){
    this.value = string;
}

// The prototypes
Example.prototype.addString = function(string){
    this.value += ' ' + string;
    return this;
};
Example.prototype.becomeArray = function(){
    return this.value.split(' ');
};

// Creating an instance of the constructor
a = new Example('This is a');
console.log(a.constructor === Example);

// Chaining methods from its prototype
a = new Example('This is a').addString('string').becomeArray();
console.log(a);

Of course this is an oversimplified example. And there is many other ways to achieve the result.

But then that is out of scope of this question.

yqlim
  • 6,898
  • 3
  • 19
  • 43