字符串不是Coffeescript中的函数

时间:2012-01-20 13:12:38

标签: javascript coffeescript

我刚刚开始学习coffeescript,它很棒,但它很棘手......

我尝试将javascript中的代码转换为coffeescript

我失败很多,在一个链接中我贴了3贴

  1. js.js是正常工作的代码
  2. cs.coffee是相同版本,但在coffeescript中
  3. 编译.js由cs编译器版本的js翻译
  4. 在翻译的js中,我在lambda中的某个地方出现了“String is not a function”的错误,返回到地图

    gist source code link

2 个答案:

答案 0 :(得分:9)

.length not 0

汇编成

.length( !0 )

答案 1 :(得分:3)

  1. coffeescript的@只是this.
  2. 的简写

    所以你原来的js在哪里:

    if (input1.val().length <= 4 ...
    

    你的coffeescript应该

    if   input1.val() <= 4 
    
    1. 如果您的原始js中有$(this),则您的coffeescript中仍需要$(this)。所以

      或@ input1.map( - &gt; this.val()。match(/ \ s + / g))。length not 0

    2. 应该是:

      or   @input1.map(-> $(this).val().match(/\s+/g)).length not 0
      

      我无法随意看到任何其他问题 - 尝试一下,让我们看看它是否有效,或者是否还有错误。

      <强> [编辑]

      还有其他问题,主要与not 0提及的问题相关,也包括在内。这是一个有效的(我认为)coffeescript:

          if input1.val() <= 4 \
          or (input1.map(-> $(this).val().match(/\s+/g)).length != 0) \
          or (input1.map(-> $(this).val().match(/[^A-Za-z0-9]/g)).length != 0)
          then input1.attr('id','error-highlight');
          else input1.attr('id','success-highlight');
      

      变成:

        (function() {
          if (input1.val() <= 4 || (input1.map(function() {
            return $(this).val().match(/\s+/g);
          }).length !== 0) || (input1.map(function() {
            return $(this).val().match(/[^A-Za-z0-9]/g);
          }).length !== 0)) {
            input1.attr('id', 'error-highlight');
          } else {
            input1.attr('id', 'success-highlight');
          }
        }).call(this);
      

      看起来是正确的。