0

我想在咖啡脚本函数的 for 循环中返回。但转译器问题错误。

breaker = {}
nativeForEach      = Array.prototype.forEach

_each = (obj, iterator, context) ->
    return if (obj == null)
    if nativeForEach and obj.forEach is nativeForEach
      obj.forEach(iterator, context)
    else if obj.length is +obj.length
      # seems the for loop and return statement is wrong
      for item, i in obj
        if iterator.call(context, item, i, obj) == breaker
          return
    else
      for key of obj
        if _has(obj, key)
          return if iterator.call(context, obj[key], key, obj) is breaker  

但是 coffee-script-redux 说:

D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:153
    throw new Error('expr: Cannot use a ' + s.type + ' as a value');
          ^
Error: expr: Cannot use a ReturnStatement as a value
    at expr (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:153:11)
    at expr (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:123:18)
    at expr (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:135:52)
    at makeReturn (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:182:35)
    at makeReturn (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:161:80)
    at makeReturn (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:165:42)
    at makeReturn (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:165:96)
    at makeReturn (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:163:91)
    at class$.exports.Compiler.expression (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:1094:20)
    at class$.<anonymous> (D:\dev\nodejs\node_modules\coffee-script-redux\lib\compiler.js:2325:86)

我以为我知道咖啡脚本循环语句,但它绝对让我感到困惑。请帮帮我!

编辑:实际上,我的代码是 underscore.js_.each方法的重写咖啡脚本版本。有人可以用咖啡脚本重写它吗?

4

0 回答 0