最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - String is not a function in Coffescript - Stack Overflow

programmeradmin3浏览0评论

i just started to learn coffeescript and it's great, but it's tricky..

i try to translate code, that worked in javascript to coffeescript

and i fail alot, in a link i posted 3 pastes

  1. js.js is original code that working
  2. cs.coffee is same version, but in coffeescript
  3. piled.js translated by cs piler version of js

in translated js i got an error of "String is not a function" somewhere in a lambda that returns to map

gist source code link

i just started to learn coffeescript and it's great, but it's tricky..

i try to translate code, that worked in javascript to coffeescript

and i fail alot, in a link i posted 3 pastes

  1. js.js is original code that working
  2. cs.coffee is same version, but in coffeescript
  3. piled.js translated by cs piler version of js

in translated js i got an error of "String is not a function" somewhere in a lambda that returns to map

gist source code link

Share Improve this question edited Dec 20, 2013 at 8:35 UiUx 9952 gold badges14 silver badges25 bronze badges asked Jan 20, 2012 at 13:12 UmrenUmren 3924 silver badges12 bronze badges 1
  • Your using two for loops when you could be using one :( – Raynos Commented Jan 20, 2012 at 14:19
Add a ment  | 

2 Answers 2

Reset to default 9

.length not 0

gets piled into

.length( !0 )

  1. coffeescript's @ is just shorthand for this.

So where your original js has:

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

your coffeescript should have

if   input1.val() <= 4 
  1. Where you have $(this) in your original js, you still need $(this) in your coffeescript. So

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

should be:

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

I can't offhand see any other issues - try it and let's see if that gets it working, or if there are still errors.

[Edit]

There were other issues, largely related as mentioned to the not 0 and also to bracketing. Here's a working (I think) 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');

It bees:

  (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);

Which looks about right.

发布评论

评论列表(0)

  1. 暂无评论