为什么这个咖啡脚本没有编译“;正确”;

Why is this coffeescript not compiling "correctly"?

本文关键字:编译 正确 咖啡 脚本 为什么      更新时间:2023-11-07

我在Rails应用程序中有以下内容:

# projects.js.coffee
jQuery ->
  $('#project_title').typing ->
    start: (event, element) ->
      element.css 'background', '#fa0'
    stop: (event, element) ->
      element.css 'background', '#f00'
      alert 'send request to populate samples'
    delay: 400

然而,在编译后的js文件中,我得到了以下内容:

(function() {
  jQuery(function() {
    return $('#sprinkler_word').typing(function() {
      ({
        start: function(event, element) {
          return element.css('background', '#fa0');
        },
        stop: function(event, element) {
          return element.css('background', '#f00');
        }
      });
      alert('send request to populate sprinkle samples');
      return {
        delay: 400
      };
    });
  });
}).call(this);

为什么"停止"功能中没有包含"警报"调用?我认为我的语法不正确,但我看不清在哪里。谢谢

将您的代码粘贴到"Try Coffeescript"小部件中,即您期望的代码,即停止功能内的警报:

jQuery(function() {
  return $('#project_title').typing(function() {
    return {
      start: function(event, element) {
        return element.css('background', '#fa0');
      },
      stop: function(event, element) {
        element.css('background', '#f00');
        return alert('send request to populate samples');
      },
      delay: 400
    };
  });
});

因此,我猜测制表符和空格会打乱缩进。