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 1 hour ago









      Swisher Sweet

      1,77311130




      1,77311130




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          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
            1 hour ago











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            1 hour ago










          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
          4
          down vote













          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
            1 hour ago











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            1 hour ago














          up vote
          4
          down vote













          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
            1 hour ago











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            1 hour ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          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 1 hour ago

























          answered 1 hour ago









          sfdcfox

          226k10173388




          226k10173388











          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            1 hour ago











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            1 hour ago
















          • I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
            – Swisher Sweet
            1 hour ago











          • @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
            – sfdcfox
            1 hour ago















          I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
          – Swisher Sweet
          1 hour ago





          I was so close. In one of my tries I wrapped the (function()...) but forgot the last (). Thank you!
          – Swisher Sweet
          1 hour ago













          @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
          – sfdcfox
          1 hour ago




          @SwisherSweet Yeah, it's kind of unusual if you're not used to using this pattern, really easy to miss.
          – sfdcfox
          1 hour ago

















           

          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













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          What does second last employer means? [closed]

          One-line joke