admin管理员组

文章数量:1289508

Here's what I want to see:

kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
    kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
        alert(returned1 + returned2);
    });
});

Here's what I wrote in coffeescript:

kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->

  kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->

    alert returned1 + returned2

The problem here is that no matter what, coffeescript is making the function ()-> return something. In that case, the last statement is being returned for some reason.

If I were to place a second alert in the nested function with returned2, it would return instead of the first one:

kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
    kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
      alert(returned1 + returned2);
      return alert('something');
    });

How to have it avoid doing the return?

Here's what I want to see:

kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
    kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
        alert(returned1 + returned2);
    });
});

Here's what I wrote in coffeescript:

kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->

  kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->

    alert returned1 + returned2

The problem here is that no matter what, coffeescript is making the function ()-> return something. In that case, the last statement is being returned for some reason.

If I were to place a second alert in the nested function with returned2, it would return instead of the first one:

kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
    kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
      alert(returned1 + returned2);
      return alert('something');
    });

How to have it avoid doing the return?

Share Improve this question edited Jan 6, 2013 at 0:15 dsp_099 asked Jan 5, 2013 at 23:56 dsp_099dsp_099 6,12120 gold badges78 silver badges131 bronze badges 7
  • 3 Hmm, I piled that and got what you expected. Are you sure the indentation was as you said? – James M Commented Jan 6, 2013 at 0:01
  • How bizarre. It seems to happen only on fiddlesalad: fiddlesalad./coffeescript/asdfasdf-asf-asf – dsp_099 Commented Jan 6, 2013 at 0:05
  • I updated the question slightly. – dsp_099 Commented Jan 6, 2013 at 0:15
  • plies for me in jsbin too. jsbin./ayejal/1/edit – zb' Commented Jan 6, 2013 at 0:20
  • Out of interest, why do you care? – Lightness Races in Orbit Commented Jan 6, 2013 at 0:22
 |  Show 2 more ments

2 Answers 2

Reset to default 11

If you don't want a function to return something then just say return:

kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->
  kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->
    alert returned1 + returned2
    return

return behaves the same in CoffeeScript as it does in JavaScript so you can say return if you don't want any particular return value.

If you don't specify an explicit return value using return, a CoffeeScript function will return the value of the last expression so your CoffeeScript is equivalent to:

kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->
  kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->
    return alert returned1 + returned2

The result will be the same though, alert doesn't return anything so:

f = ->
    alert 'x'
    return
x = f()

will give undefined in x but so will this:

f = -> alert 'x'
x = f()

In coffeescript a function always returns the final statement. You can explicitly make a function return undefined by making that the final statement.

kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->

  kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->

    alert returned1 + returned2
    `undefined`

本文标签: javascriptHow to avoid this return in nested coffeescriptStack Overflow