Javascript ExecJS::ProgramError: SyntaxError: 保留字“函数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9011103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:23:04  来源:igfitidea点击:

ExecJS::ProgramError: SyntaxError: Reserved word "function"

javascriptjqueryruby-on-railscoffeescript

提问by user938363

In our rails rfq.js.coffee, we only have a simple js code:

在我们的 rails rfq.js.coffee 中,我们只有一个简单的 js 代码:

$(function() {
  $('#need_report').change(function(){
    if ($(this).val() == true) {
      $('#report_language').hide();
    }  // end if
  });  // end change()
});  // end ready(function)

However this code causes an error saying that function() in first line is a reserved word. Since the first line is basically a jquery $(document).ready(function () {}), we have no clue why this error shows up. Any thoughts about it? Thanks so much.

然而,这段代码会导致一个错误,指出第一行中的 function() 是一个保留字。由于第一行基本上是一个 jquery $(document).ready(function () {}),我们不知道为什么会出现这个错误。有什么想法吗?非常感谢。

回答by Dylan Markow

You can't use standard JS like that in a Coffeescript file. Either rename the file to rfq.js, or convert it to coffeescript:

你不能在 Coffeescript 文件中使用这样的标准 JS。将文件重命名为rfq.js,或将其转换为 coffeescript:

$ ->
  $('#need_report').change ->
    if $(this).val()
      $('#report_language').hide()

回答by hourwise

You can embed regular javascript by surrounding the code with back-ticks "`". I wish it worked like the other parsing languages as well...it took me lot's of unnecessary debugging and searching to figure that out. http://coffeescript.org/#embedded

您可以通过用反引号“`”包围代码来嵌入常规 javascript。我希望它也能像其他解析语言一样工作……我花了很多不必要的调试和搜索来弄清楚这一点。 http://coffeescript.org/#embedded

回答by hguzman

Maybe you wrote JavaScript code into file with extension .coffeeyou can use js2.coffeeto convert your code from JavaScript to CoffeeSecript

也许您将 JavaScript 代码写入带有扩展名的文件中,.coffee您可以使用js2.coffee将您的代码从 JavaScript 转换为 CoffeeSecript