0

I can store a function in a variable and call it later like this

var storedFunction = functionName;

//Later On
storedFunction(); //Equivalent to functionName()

How can I store a function like this and call it later, also executing this when its called so this would be the

var storedFunction = this.function1().function2();

Later I could call it like this and this would be the class.

class MyClass {
    method() {
        storedFunction();
    }
}
Jack
  • 1,422
  • 8
  • 21
  • `var storedFunction = function1.bind(this);` will lock the `this` context of where this line is. You can then call `storedFunction()` from anywhere, while keeping the original context – blex Dec 03 '20 at 20:54
  • Any way I can execute `function2` also? – Jack Dec 03 '20 at 20:56
  • if `function1` returns an Object (or instance of a class) with a `"function2"` property, yes you can. **Edit**: maybe you meant to store the whole thing? `var storedFunction = (function() { return this.function1().function2(); }).bind(this);` – blex Dec 03 '20 at 20:56
  • Ok, when I tried it, `bind(this)`, this acted like normal and used the object while it was being defined. How can I delay to when it is called? – Jack Dec 03 '20 at 21:06
  • Ok, I did not understand that from your original question. Then, you could do `var storedFunction = function() { return this.function1().function2(); };` (without the bind). And then, in your class, call it like so: `storedFunction.call(this, /* parameters */);` – blex Dec 03 '20 at 21:17
  • Does this answer your question? [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – FZs Dec 03 '20 at 21:33

2 Answers2

1

If you have a function like this :

function myFunction(){
    return {
        function inner(){
            return 'some data'
        }
    }
}

When you call myFunction with myFunction() returned value is an object like this:

{
    inner: function (){ return 'some data'; }  
}

If you want to store inner function just need store your value with it's key from returned object, like this:

let myInnerFunction = myFunction().inner;

...

//and when you want call that
myInnerFunction ()

In your case this is correct:

let storedFunction = this.function1().function2;

If you want to run function1 and function2 when you call there is a simple way:

let storedFunction = () => {
    return this.function1().function2();
}

// or without arrow function

let storedFunction = function () {
    return this.function1().function2();
}.bind(this)

alefseen
  • 91
  • 5
  • Hi, thanks for your answer. If I understand your code correctly, your last bit will still execute `this.function1()` on definition, and then execute `function2()` on call. I need to call both `function1()` and `function2()` as well as `this`, if possible – Jack Dec 03 '20 at 21:25
  • answer is updated – alefseen Dec 03 '20 at 21:39
1

The dot operator (.) is the object access operator. That means whenever you see it (not related to a number), the code is attempting to access a property or method (function) of that object.

That means: this.something is attempting to access the something property on an object referred to as this.

Related, then function1().function2() means function2() must be a method of an object too. But, in this case, the object must be returned from function1() in order for this to work.

So here is some sample code that would do what you expect.

function function1() {
  return {
    function2: () => {
      console.log('called function2 from function1 on "this"');
    }
  }
}
function1.bind(this);
this.function1().function2();

// Using class syntax

class OtherClass {
  function2() {
    console.log('Used class keyword');
  }
}

class MyClass {

  constructor() {
    this.veryOddThingToDo = this.function1().function2;
  }

  function1() {
    return new OtherClass();
  }
}

const myClass = new MyClass();
// Get reference to function2() from "MyClass.function1()" call
const oddness = myClass.veryOddThingToDo;

// This is exactly: this.function1().function2() referenced"
oddness();
Randy Casburn
  • 11,404
  • 1
  • 12
  • 26
  • Thanks for your answer. How do I store your last line so that I can call within an instance of `class` that `function1()` belongs to (which then returns a 2nd instance of a class to which `function2()` belongs)? – Jack Dec 03 '20 at 21:22
  • @Jack Please see the updated answer with both solutions. – Randy Casburn Dec 03 '20 at 21:31
  • Thanks, is it possible to store `this.function1().function2();` in a variable and then call it late with something like `storedFunction()`? – Jack Dec 03 '20 at 21:36
  • @Jack Yes - see updated answer - but I have no idea why you would want to do this! – Randy Casburn Dec 03 '20 at 21:40
  • @Jack - please consider: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Randy Casburn Dec 03 '20 at 22:00
  • I definitely will. Thanks for your time. But as you said, I have a very weird problem. I apologize for my communication errors. Alfefseen was able to answer my question the, however. – Jack Dec 03 '20 at 22:06
  • @Jack - glad you got it sorted out. – Randy Casburn Dec 03 '20 at 22:09