admin管理员组文章数量:1353322
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
- js.js is original code that working
- cs.coffee is same version, but in coffeescript
- 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
- js.js is original code that working
- cs.coffee is same version, but in coffeescript
- 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
2 Answers
Reset to default 9.length not 0
gets piled into
.length( !0 )
- coffeescript's
@
is just shorthand forthis.
So where your original js has:
if (input1.val().length <= 4 ...
your coffeescript should have
if input1.val() <= 4
Where you have
$(this)
in your original js, you still need$(this)
in your coffeescript. Soor @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.
本文标签: javascriptString is not a function in CoffescriptStack Overflow
版权声明:本文标题:javascript - String is not a function in Coffescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743927178a2563176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论