Javascript function evaluates to true even when it returns false in Lightning Helper

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












I am trying to encapsulate logic to determine if a button is visible inside the second argument via anonymous function:



component.set("!v.assignButtonVisible", function() 
console.log('Do we get here?');
return false;
);


However, the code inside the function never executes and the attribute is set to true.



The only reason I can think of is at run-time, when function() ... resolves, it is not null and therefore is interpreted as true.



Is what I'm trying to do possible? Can I call a named or anonymous function to get the value I'm looking for, or is my only option to create and set a var before calling component.set( ... )?










share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    I am trying to encapsulate logic to determine if a button is visible inside the second argument via anonymous function:



    component.set("!v.assignButtonVisible", function() 
    console.log('Do we get here?');
    return false;
    );


    However, the code inside the function never executes and the attribute is set to true.



    The only reason I can think of is at run-time, when function() ... resolves, it is not null and therefore is interpreted as true.



    Is what I'm trying to do possible? Can I call a named or anonymous function to get the value I'm looking for, or is my only option to create and set a var before calling component.set( ... )?










    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I am trying to encapsulate logic to determine if a button is visible inside the second argument via anonymous function:



      component.set("!v.assignButtonVisible", function() 
      console.log('Do we get here?');
      return false;
      );


      However, the code inside the function never executes and the attribute is set to true.



      The only reason I can think of is at run-time, when function() ... resolves, it is not null and therefore is interpreted as true.



      Is what I'm trying to do possible? Can I call a named or anonymous function to get the value I'm looking for, or is my only option to create and set a var before calling component.set( ... )?










      share|improve this question













      I am trying to encapsulate logic to determine if a button is visible inside the second argument via anonymous function:



      component.set("!v.assignButtonVisible", function() 
      console.log('Do we get here?');
      return false;
      );


      However, the code inside the function never executes and the attribute is set to true.



      The only reason I can think of is at run-time, when function() ... resolves, it is not null and therefore is interpreted as true.



      Is what I'm trying to do possible? Can I call a named or anonymous function to get the value I'm looking for, or is my only option to create and set a var before calling component.set( ... )?







      lightning-components javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 13 at 16:11









      Swisher Sweet

      1,79511131




      1,79511131




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          You actually put the function in to the attribute, not the return value, because the function was never "called" (executed). If you want to call such a function inline, you can, but you need to actually call it. This just requires a few more parentheses:



          component.set("!v.assignButtonVisible", (function() 
          console.log('Do we get here?');
          return false;
          )());


          We put the entire function inside parentheses, then give it an empty parameter list. That pattern looks like:



          (function(...) ... )(...)


          You can put stuff in where the dots are (parameters and code body). This is called the "self-executing anonymous function" pattern.






          share|improve this answer






















          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            Sep 13 at 16:19











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            Sep 13 at 16:21










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f232393%2fjavascript-function-evaluates-to-true-even-when-it-returns-false-in-lightning-he%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          You actually put the function in to the attribute, not the return value, because the function was never "called" (executed). If you want to call such a function inline, you can, but you need to actually call it. This just requires a few more parentheses:



          component.set("!v.assignButtonVisible", (function() 
          console.log('Do we get here?');
          return false;
          )());


          We put the entire function inside parentheses, then give it an empty parameter list. That pattern looks like:



          (function(...) ... )(...)


          You can put stuff in where the dots are (parameters and code body). This is called the "self-executing anonymous function" pattern.






          share|improve this answer






















          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            Sep 13 at 16:19











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            Sep 13 at 16:21














          up vote
          5
          down vote



          accepted










          You actually put the function in to the attribute, not the return value, because the function was never "called" (executed). If you want to call such a function inline, you can, but you need to actually call it. This just requires a few more parentheses:



          component.set("!v.assignButtonVisible", (function() 
          console.log('Do we get here?');
          return false;
          )());


          We put the entire function inside parentheses, then give it an empty parameter list. That pattern looks like:



          (function(...) ... )(...)


          You can put stuff in where the dots are (parameters and code body). This is called the "self-executing anonymous function" pattern.






          share|improve this answer






















          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            Sep 13 at 16:19











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            Sep 13 at 16:21












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          You actually put the function in to the attribute, not the return value, because the function was never "called" (executed). If you want to call such a function inline, you can, but you need to actually call it. This just requires a few more parentheses:



          component.set("!v.assignButtonVisible", (function() 
          console.log('Do we get here?');
          return false;
          )());


          We put the entire function inside parentheses, then give it an empty parameter list. That pattern looks like:



          (function(...) ... )(...)


          You can put stuff in where the dots are (parameters and code body). This is called the "self-executing anonymous function" pattern.






          share|improve this answer














          You actually put the function in to the attribute, not the return value, because the function was never "called" (executed). If you want to call such a function inline, you can, but you need to actually call it. This just requires a few more parentheses:



          component.set("!v.assignButtonVisible", (function() 
          console.log('Do we get here?');
          return false;
          )());


          We put the entire function inside parentheses, then give it an empty parameter list. That pattern looks like:



          (function(...) ... )(...)


          You can put stuff in where the dots are (parameters and code body). This is called the "self-executing anonymous function" pattern.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 13 at 16:19

























          answered Sep 13 at 16:15









          sfdcfox

          230k10177391




          230k10177391











          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            Sep 13 at 16:19











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            Sep 13 at 16:21
















          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            Sep 13 at 16:19











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            Sep 13 at 16:21















          I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
          – Swisher Sweet
          Sep 13 at 16:19





          I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
          – Swisher Sweet
          Sep 13 at 16:19













          @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
          – sfdcfox
          Sep 13 at 16:21




          @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
          – sfdcfox
          Sep 13 at 16:21

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f232393%2fjavascript-function-evaluates-to-true-even-when-it-returns-false-in-lightning-he%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Christian Cage

          How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?