回答:
原来我new在创建类实例时忽略了使用关键字。问题本身的代码很好。
问题:
我有一个相当简单的类,其中构造函数调用类上的另一个方法(editor_for_node)。调用发生在 jQuery each() 循环内,但我也尝试将它移到外面。
define ['jquery'], ($) ->
  class Editor
    constructor: (@node, @data, @template) ->
      @node.widgets().each (i, elem) =>
        data = if @data then @data[i] else null
        node = $(elem)
        @editor_for_node node, data
    editor_for_node: (node, data) ->
      console.log 'hello!'
  return {
    'Editor': Editor,
  }
当该行@editor_for_node node, data被调用时,我收到一个错误(在 Firebug 中)说this.editor_for_node is not a function.
我真的不明白为什么这不能正常工作,我能看到的唯一可能的奇怪来源是我在一开始就使用了 require.js 的定义函数。
编辑:生成的输出
(function() {
  define(['jquery'], function($) {
    var Editor;
    Editor = (function() {
      Editor.name = 'Editor';
      function Editor(node, data, template) {
        var _this = this;
        this.node = node;
        this.data = data;
        this.template = template;
        this.node.widgets().each(function(i, elem) {
          data = _this.data ? _this.data[i] : null;
          node = $(elem);
          return _this.editor_for_node(node, data);
        });
      }
      Editor.prototype.editor_for_node = function(node, data) {
        return console.log('hello!');
      };
      return Editor;
    })();
    return {
      'Editor': Editor
    };
  });
}).call(this);