0

Coffeescript insists on adding a return statement, how do I get rid of it?

Coffeescript:

  $('.box-menu').each (index, value) ->
    @boxes.push new LW.Box.Box @ajax, $(this).data('type')

Javascript after compilation:

$('.box-menu').each(function(index, value) {
  return this.boxes.push(new LW.Box.Box(this.ajax, $(this).data('type')));
});
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131

2 Answers2

2

You can't really, but you can structure the entire thing differently, since there's no real use for jQuery.each here to begin with:

@boxes.push new LW.Box.Box @ajax, $(elem).data('type') for elem in $('.box-menu')

Even if you don't do that, there should be absolutely no issue with the return statement here, which is why Coffeescript makes everything an expression in the first place: usually it makes no difference.

If absolutely necessary, you could do this:

$('.box-menu').each `function () {
  this.boxes.push(new LW.Box.Box(this.ajax, $(this).data('type')));
}`
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Technically doesn't answer the question, but I agree it's the nicer way of doing it! – Matheus208 Oct 29 '15 at 14:41
  • Thank you. This cleared up a lot of my questions. I like your first solution. – kemicofa ghost Oct 29 '15 at 14:49
  • Using raw JS in CoffeeScript is a very bad practice. CS covers ECMA5 completely, so anything you could do in raw JS could also be done in CS. – Leonid Beschastny Oct 29 '15 at 14:52
  • And you're wrong telling OP that he can't get rid of `return`, because he can (see [**Matheus208**'s answer](http://stackoverflow.com/a/33417210/1202461)). – Leonid Beschastny Oct 29 '15 at 14:55
  • @Leonid I wouldn't really call that "getting rid of"; it's just *explictly returning a defined valued* instead of an *implicit return*... It compiles to no return statement, but that's rather coincidental. – deceze Oct 29 '15 at 15:12
1
 $('.box-menu').each (index, value) ->
    @boxes.push new LW.Box.Box @ajax, $(this).data('type')
    return
Matheus208
  • 1,289
  • 2
  • 13
  • 26