Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I prevent these unwanted returns in my if/else block?
    text
    copied!<p><strong>Update:</strong> My problem was caused by two functions returning and not just one. The callback function in the <code>$.each</code> loop was generating the returns in the <code>if/else</code> block. Even though I link it later in the question, there is a good question about <a href="https://stackoverflow.com/questions/7391493/is-there-any-way-to-not-return-something-using-coffeescript">how to override returns</a> when CoffeeScript compiles to JavaScript. This is not a duplicate of that question; I had a more specific problem.</p> <p>I'm trying to learn CoffeeScript for fun by porting some work I did in JavaScript with the <a href="http://jqueryui.com/widget/" rel="nofollow noreferrer">jQuery Widget Factory</a>. Assume <code>$foos</code> is a boolean array. I have a CoffeeScript function that looks similar to this:</p> <pre><code>$ = jQuery activateCats = ($foos) -&gt; $.each $foos, (idx, foo) -&gt; $bar = $(".cats").eq(idx) if foo $bar.addClass("ui-state-active") .removeClass "ui-state-disabled" else $bar.removeClass("ui-state-active") .addClass "ui-state-disabled" # add another empty return here return </code></pre> <p>The compiled JavaScript looks like this:</p> <pre><code>var $, doThings; $ = jQuery; activateCats = function($foos) { // Function 1 $.each($foos, function(idx, foo) { // Function 2 var $bar; $bar = $(".cats").eq(idx); if (foo) { return $bar.addClass("ui-state-active").removeClass("ui-state-disabled"); } else { return $bar.removeClass("ui-state-active").addClass("ui-state-disabled"); } }); }; </code></pre> <p>If I don't include the return at the bottom it compiles to this (note the return in front of my <code>$.each</code> loop):</p> <pre><code>var $, doThings; $ = jQuery; activateCats = function($foos) { return $.each($foos, function(idx, foo) { var $bar; $bar = $(".cats").eq(idx); if (foo) { return $bar.addClass("ui-state-active").removeClass("ui-state-disabled"); } else { return $bar.removeClass("ui-state-active").addClass("ui-state-disabled"); } }); }; </code></pre> <p>I'm pretty sure I don't want those returns to be in my <code>$.each</code> loop. I'm not sure why CoffeeScript is adding them, because I have the empty <code>return</code> at the bottom of my function. I was having this problem before in other places but I read <a href="https://stackoverflow.com/questions/7391493/is-there-any-way-to-not-return-something-using-coffeescript">this thread</a> and solved those issues. I've tried the various methods in that thread and none of them seem to work for this edge case.</p> <p><strong>How can I prevent CoffeeScript from adding these returns?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload