JavaScript: Access 'this' when calling function stored in variableStop setInterval call in JavaScriptWhen to use double or single quotes in JavaScript?What is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Set a default parameter value for a JavaScript functionCheck if a variable is a string in JavaScriptJavaScript check if variable exists (is defined/initialized)Is there a standard function to check for null, undefined, or blank variables in JavaScript?indexOf method in an object array?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it

number headings

Teacher help me explain this to my students

Gladys goes shopping

Sitecore 9.0 works with solr 7.2.1?

Apache redirect to https:/www only partially working

What was the idiom for something that we take without a doubt?

Who will lead the country until there is a new Tory leader?

What are these arcade games in Ghostbusters 1984?

I unknowingly submitted plagarised work

Is it true that cut time means "play twice as fast as written"?

NIntegrate doesn't evaluate

Pirate democracy at its finest

How did these characters "suit up" so quickly?

using Leibniz rule to solve definite integral

What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?

What to do when you've set the wrong ISO for your film?

Count rotary dial pulses in a phone number (including letters)

How to use libraries with delays inside within a time critical STM32 HAL application?

Externally monitoring CPU/SSD activity without software access

How do I partition a matrx into blocks and replace zeros with dots?

How to Pin Point Large File eating space in Fedora 18

Where's this lookout in Nova Scotia?

Is it possible to play as a necromancer skeleton?

Looking for a soft substance that doesn't dissolve underwater



JavaScript: Access 'this' when calling function stored in variable


Stop setInterval call in JavaScriptWhen to use double or single quotes in JavaScript?What is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Set a default parameter value for a JavaScript functionCheck if a variable is a string in JavaScriptJavaScript check if variable exists (is defined/initialized)Is there a standard function to check for null, undefined, or blank variables in JavaScript?indexOf method in an object array?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








11















I'm new to JavaScript so this is possibly a trivial question:



I'm trying to construct an object that stores a mapping from a set of integers to some of its methods, i.e. something like this:



'use strict';

function Foo()
this.funcs =
1: this.func1,
2: this.func2,



Foo.prototype.func1 = function()
this.prop = 1;


Foo.prototype.func2 = function()
this.prop = 2;



I'd then like to be able to call methods of Foo like this:



foo = new Foo();
var func = foo.funcs[1];
func();


But this results in: Cannot set property 'prop' of undefined, i.e. this does not refer to foo.



What's the problem here and is there a better way to implement this?










share|improve this question






























    11















    I'm new to JavaScript so this is possibly a trivial question:



    I'm trying to construct an object that stores a mapping from a set of integers to some of its methods, i.e. something like this:



    'use strict';

    function Foo()
    this.funcs =
    1: this.func1,
    2: this.func2,



    Foo.prototype.func1 = function()
    this.prop = 1;


    Foo.prototype.func2 = function()
    this.prop = 2;



    I'd then like to be able to call methods of Foo like this:



    foo = new Foo();
    var func = foo.funcs[1];
    func();


    But this results in: Cannot set property 'prop' of undefined, i.e. this does not refer to foo.



    What's the problem here and is there a better way to implement this?










    share|improve this question


























      11












      11








      11


      1






      I'm new to JavaScript so this is possibly a trivial question:



      I'm trying to construct an object that stores a mapping from a set of integers to some of its methods, i.e. something like this:



      'use strict';

      function Foo()
      this.funcs =
      1: this.func1,
      2: this.func2,



      Foo.prototype.func1 = function()
      this.prop = 1;


      Foo.prototype.func2 = function()
      this.prop = 2;



      I'd then like to be able to call methods of Foo like this:



      foo = new Foo();
      var func = foo.funcs[1];
      func();


      But this results in: Cannot set property 'prop' of undefined, i.e. this does not refer to foo.



      What's the problem here and is there a better way to implement this?










      share|improve this question
















      I'm new to JavaScript so this is possibly a trivial question:



      I'm trying to construct an object that stores a mapping from a set of integers to some of its methods, i.e. something like this:



      'use strict';

      function Foo()
      this.funcs =
      1: this.func1,
      2: this.func2,



      Foo.prototype.func1 = function()
      this.prop = 1;


      Foo.prototype.func2 = function()
      this.prop = 2;



      I'd then like to be able to call methods of Foo like this:



      foo = new Foo();
      var func = foo.funcs[1];
      func();


      But this results in: Cannot set property 'prop' of undefined, i.e. this does not refer to foo.



      What's the problem here and is there a better way to implement this?







      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 19 at 22:35









      Dacre Denny

      15.9k41433




      15.9k41433










      asked May 19 at 22:14









      PeterPeter

      531311




      531311






















          2 Answers
          2






          active

          oldest

          votes


















          5














          There are a few ways to achieve what you require, however the most robust approach is to bind() each function to the instance of Foo() that is being instantiated.



          This can be done by passing this to bind() of each function:



          this.func1.bind(this)


          Using bind() in this way ensures that this, for func1 and func2 is defined as the instance of Foo(). This in turn ensures that this.prop can be accessed and assigned as expected:






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          Another key thing to note is the bind() based approach above ensures that, if you acquire and call a reference to one of the functions on the funcs field as shown in your original post, that it will work as expected:



          /* As per original post - doing this is not possible without .bind() */
          var func = foo.funcs[1];
          func();


          Without the use of bind(), this method of acquiring and calling func will fail due to func not being bound to the instance of Foo.






          share|improve this answer

























          • Very clear answer, thanks!

            – Peter
            May 19 at 22:24











          • You're welcome - glad I could help :)

            – Dacre Denny
            May 19 at 22:34


















          7














          Your problem is this line:



          var func = foo.funcs[1];


          JavaScript determines the value of this based on how a function is called. If you use dot notation, such as foo.funcs[1](); then the value of this will associated with the foo object. But when you run func(), that's just a plain function and this will have the default value of undefined.



          It would be worth your time to read the two chapters of You Don't Know JS that discuss this. It should take less than an hour to learn, and you'll be way ahead of most JS programmers once you learn it.



          The rules might not make sense until you read the chapter, but they are summarized below:




          Determining the this binding for an executing function requires
          finding the direct call-site of that function. Once examined, four
          rules can be applied to the call-site, in this order of precedence:



          Called with new? Use the newly constructed object.



          Called with call or apply (or bind)? Use the specified object.



          Called with a context object owning the call? Use that context object.



          Default: undefined in strict mode, global object otherwise.




          Based on the above rules, the code below is the simplest way you could get it to work the way you are expecting it to:






          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();








          share|improve this answer























          • @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

            – Todd Chaffee
            May 20 at 13:19











          • Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

            – Dacre Denny
            May 20 at 20:21












          • You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

            – Todd Chaffee
            May 20 at 21:06











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56212424%2fjavascript-access-this-when-calling-function-stored-in-variable%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          There are a few ways to achieve what you require, however the most robust approach is to bind() each function to the instance of Foo() that is being instantiated.



          This can be done by passing this to bind() of each function:



          this.func1.bind(this)


          Using bind() in this way ensures that this, for func1 and func2 is defined as the instance of Foo(). This in turn ensures that this.prop can be accessed and assigned as expected:






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          Another key thing to note is the bind() based approach above ensures that, if you acquire and call a reference to one of the functions on the funcs field as shown in your original post, that it will work as expected:



          /* As per original post - doing this is not possible without .bind() */
          var func = foo.funcs[1];
          func();


          Without the use of bind(), this method of acquiring and calling func will fail due to func not being bound to the instance of Foo.






          share|improve this answer

























          • Very clear answer, thanks!

            – Peter
            May 19 at 22:24











          • You're welcome - glad I could help :)

            – Dacre Denny
            May 19 at 22:34















          5














          There are a few ways to achieve what you require, however the most robust approach is to bind() each function to the instance of Foo() that is being instantiated.



          This can be done by passing this to bind() of each function:



          this.func1.bind(this)


          Using bind() in this way ensures that this, for func1 and func2 is defined as the instance of Foo(). This in turn ensures that this.prop can be accessed and assigned as expected:






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          Another key thing to note is the bind() based approach above ensures that, if you acquire and call a reference to one of the functions on the funcs field as shown in your original post, that it will work as expected:



          /* As per original post - doing this is not possible without .bind() */
          var func = foo.funcs[1];
          func();


          Without the use of bind(), this method of acquiring and calling func will fail due to func not being bound to the instance of Foo.






          share|improve this answer

























          • Very clear answer, thanks!

            – Peter
            May 19 at 22:24











          • You're welcome - glad I could help :)

            – Dacre Denny
            May 19 at 22:34













          5












          5








          5







          There are a few ways to achieve what you require, however the most robust approach is to bind() each function to the instance of Foo() that is being instantiated.



          This can be done by passing this to bind() of each function:



          this.func1.bind(this)


          Using bind() in this way ensures that this, for func1 and func2 is defined as the instance of Foo(). This in turn ensures that this.prop can be accessed and assigned as expected:






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          Another key thing to note is the bind() based approach above ensures that, if you acquire and call a reference to one of the functions on the funcs field as shown in your original post, that it will work as expected:



          /* As per original post - doing this is not possible without .bind() */
          var func = foo.funcs[1];
          func();


          Without the use of bind(), this method of acquiring and calling func will fail due to func not being bound to the instance of Foo.






          share|improve this answer















          There are a few ways to achieve what you require, however the most robust approach is to bind() each function to the instance of Foo() that is being instantiated.



          This can be done by passing this to bind() of each function:



          this.func1.bind(this)


          Using bind() in this way ensures that this, for func1 and func2 is defined as the instance of Foo(). This in turn ensures that this.prop can be accessed and assigned as expected:






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          Another key thing to note is the bind() based approach above ensures that, if you acquire and call a reference to one of the functions on the funcs field as shown in your original post, that it will work as expected:



          /* As per original post - doing this is not possible without .bind() */
          var func = foo.funcs[1];
          func();


          Without the use of bind(), this method of acquiring and calling func will fail due to func not being bound to the instance of Foo.






          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();





          'use strict';

          function Foo()
          this.funcs =
          /* Bind the functions to this Foo() instance */
          1: this.func1.bind(this),
          2: this.func2.bind(this),



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          var func = foo.funcs[1];
          func();

          foo.funcs[2]();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 20 at 7:39

























          answered May 19 at 22:20









          Dacre DennyDacre Denny

          15.9k41433




          15.9k41433












          • Very clear answer, thanks!

            – Peter
            May 19 at 22:24











          • You're welcome - glad I could help :)

            – Dacre Denny
            May 19 at 22:34

















          • Very clear answer, thanks!

            – Peter
            May 19 at 22:24











          • You're welcome - glad I could help :)

            – Dacre Denny
            May 19 at 22:34
















          Very clear answer, thanks!

          – Peter
          May 19 at 22:24





          Very clear answer, thanks!

          – Peter
          May 19 at 22:24













          You're welcome - glad I could help :)

          – Dacre Denny
          May 19 at 22:34





          You're welcome - glad I could help :)

          – Dacre Denny
          May 19 at 22:34













          7














          Your problem is this line:



          var func = foo.funcs[1];


          JavaScript determines the value of this based on how a function is called. If you use dot notation, such as foo.funcs[1](); then the value of this will associated with the foo object. But when you run func(), that's just a plain function and this will have the default value of undefined.



          It would be worth your time to read the two chapters of You Don't Know JS that discuss this. It should take less than an hour to learn, and you'll be way ahead of most JS programmers once you learn it.



          The rules might not make sense until you read the chapter, but they are summarized below:




          Determining the this binding for an executing function requires
          finding the direct call-site of that function. Once examined, four
          rules can be applied to the call-site, in this order of precedence:



          Called with new? Use the newly constructed object.



          Called with call or apply (or bind)? Use the specified object.



          Called with a context object owning the call? Use that context object.



          Default: undefined in strict mode, global object otherwise.




          Based on the above rules, the code below is the simplest way you could get it to work the way you are expecting it to:






          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();








          share|improve this answer























          • @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

            – Todd Chaffee
            May 20 at 13:19











          • Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

            – Dacre Denny
            May 20 at 20:21












          • You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

            – Todd Chaffee
            May 20 at 21:06















          7














          Your problem is this line:



          var func = foo.funcs[1];


          JavaScript determines the value of this based on how a function is called. If you use dot notation, such as foo.funcs[1](); then the value of this will associated with the foo object. But when you run func(), that's just a plain function and this will have the default value of undefined.



          It would be worth your time to read the two chapters of You Don't Know JS that discuss this. It should take less than an hour to learn, and you'll be way ahead of most JS programmers once you learn it.



          The rules might not make sense until you read the chapter, but they are summarized below:




          Determining the this binding for an executing function requires
          finding the direct call-site of that function. Once examined, four
          rules can be applied to the call-site, in this order of precedence:



          Called with new? Use the newly constructed object.



          Called with call or apply (or bind)? Use the specified object.



          Called with a context object owning the call? Use that context object.



          Default: undefined in strict mode, global object otherwise.




          Based on the above rules, the code below is the simplest way you could get it to work the way you are expecting it to:






          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();








          share|improve this answer























          • @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

            – Todd Chaffee
            May 20 at 13:19











          • Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

            – Dacre Denny
            May 20 at 20:21












          • You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

            – Todd Chaffee
            May 20 at 21:06













          7












          7








          7







          Your problem is this line:



          var func = foo.funcs[1];


          JavaScript determines the value of this based on how a function is called. If you use dot notation, such as foo.funcs[1](); then the value of this will associated with the foo object. But when you run func(), that's just a plain function and this will have the default value of undefined.



          It would be worth your time to read the two chapters of You Don't Know JS that discuss this. It should take less than an hour to learn, and you'll be way ahead of most JS programmers once you learn it.



          The rules might not make sense until you read the chapter, but they are summarized below:




          Determining the this binding for an executing function requires
          finding the direct call-site of that function. Once examined, four
          rules can be applied to the call-site, in this order of precedence:



          Called with new? Use the newly constructed object.



          Called with call or apply (or bind)? Use the specified object.



          Called with a context object owning the call? Use that context object.



          Default: undefined in strict mode, global object otherwise.




          Based on the above rules, the code below is the simplest way you could get it to work the way you are expecting it to:






          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();








          share|improve this answer













          Your problem is this line:



          var func = foo.funcs[1];


          JavaScript determines the value of this based on how a function is called. If you use dot notation, such as foo.funcs[1](); then the value of this will associated with the foo object. But when you run func(), that's just a plain function and this will have the default value of undefined.



          It would be worth your time to read the two chapters of You Don't Know JS that discuss this. It should take less than an hour to learn, and you'll be way ahead of most JS programmers once you learn it.



          The rules might not make sense until you read the chapter, but they are summarized below:




          Determining the this binding for an executing function requires
          finding the direct call-site of that function. Once examined, four
          rules can be applied to the call-site, in this order of precedence:



          Called with new? Use the newly constructed object.



          Called with call or apply (or bind)? Use the specified object.



          Called with a context object owning the call? Use that context object.



          Default: undefined in strict mode, global object otherwise.




          Based on the above rules, the code below is the simplest way you could get it to work the way you are expecting it to:






          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();








          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();





          'use strict';

          function Foo()
          this.funcs =
          1: this.func1,
          2: this.func2,



          Foo.prototype.func1 = function()
          this.prop = 1;
          console.log('called func1. this.prop =', this.prop);


          Foo.prototype.func2 = function()
          this.prop = 2;
          console.log('called func2. this.prop =', this.prop);



          const foo = new Foo();
          foo.funcs[1]();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 19 at 22:30









          Todd ChaffeeTodd Chaffee

          4,4542133




          4,4542133












          • @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

            – Todd Chaffee
            May 20 at 13:19











          • Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

            – Dacre Denny
            May 20 at 20:21












          • You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

            – Todd Chaffee
            May 20 at 21:06

















          • @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

            – Todd Chaffee
            May 20 at 13:19











          • Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

            – Dacre Denny
            May 20 at 20:21












          • You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

            – Todd Chaffee
            May 20 at 21:06
















          @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

          – Todd Chaffee
          May 20 at 13:19





          @DacreDenny yes that would fail. I thought I explained why in my answer? Without using dot notation you lose the contextual binding and it just becomes a regular function call. Is it unclear?

          – Todd Chaffee
          May 20 at 13:19













          Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

          – Dacre Denny
          May 20 at 20:21






          Hi there, it's a great answer - it just seemed that the solution for calling the function as requested in the op wasn't provided, but besides that it's very helpful 🙂

          – Dacre Denny
          May 20 at 20:21














          You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

          – Todd Chaffee
          May 20 at 21:06





          You already provided a solution using a reassigned function, so there was no reason for me to repeat that information.

          – Todd Chaffee
          May 20 at 21:06

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56212424%2fjavascript-access-this-when-calling-function-stored-in-variable%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form