简体   繁体   English

如何检查coffeescript中的语法错误

[英]How to check syntax error in coffeescript

I am not familiar with coffee script, I try to move jquery from view to put in asset but not able to make it work.我不熟悉咖啡脚本,我尝试将 jquery 从视图移动到资产中,但无法使其工作。

Here the working from view:这里的工作观点:

- jquery_ready do
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change(function() {
  var exist_id = $(this).val();
  var ids = $('#voucher_false_ids_')[0].value;
  if(jQuery.inArray(exist_id, ids.split(" ")) !== -1){
  $('label[for=voucher_name], input#voucher_name').hide();
  }
  else
  {
  $('label[for=voucher_name], input#voucher_name').show();
  }
  });
                                                                                                              

Then in /app/assets/javascript/mycode.js.coffee然后在 /app/assets/javascript/mycode.js.coffee

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();

So far, I able to run until .change -> alert('alert');到目前为止,我可以一直运行到 .change -> alert('alert'); Not after I start put all line after If不是在我开始把所有行放在 If 之后

which cause error:导致错误:

ExecJS::RuntimeError at /admin
SyntaxError: [stdin]:6:51: unexpected =

Help: for proper syntax or what is the error coming from /Thanks帮助:正确的语法或错误来自/谢谢

If you paste that snippet into the "Try CoffeScript" page of the CoffeeScript website, it tells you what's wrong.如果您将该代码段粘贴到 CoffeeScript 网站的“Try CoffeScript”页面中,它会告诉您出了什么问题。

It says this:它是这样说的:

[stdin]:7:51: error: unexpected =
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
                                                  ^

CoffeeScript does not have === or !== operators. CoffeeScript 没有===!==运算符。 == and != are translated their strict equivalents when compiled for you. ==!=在为您编译时被翻译为严格的等价物。

If you fix that, you get another error:如果你解决了这个问题,你会得到另一个错误:

[stdin]:8:1: error: unexpected indentation
      $('label[for=voucher_name], input#voucher_name').hide();
^^^^^^

This is because of the previous line.这是因为上一行。 You start an if statement with If , so CoffeeScript does not see this as an if statement, but instead as a function call to the If() function.您使用If开始if语句,因此 CoffeeScript 不会将其视为if语句,而是将其视为对If()函数的函数调用。

Change that to lowercase if and then it compiles just fine.它改成小写if然后它编译就好了。


Short version, here it is fixed:简短版本,这里是固定的:

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    if(jQuery.inArray(exist_id, ids.split(" ")) != -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM