Class 咖啡脚本未定义类?

Class 咖啡脚本未定义类?,class,methods,coffeescript,visibility,Class,Methods,Coffeescript,Visibility,我从咖啡脚本开始。(还有英语,所以对于语法错误我很抱歉。)看这节课: class Stuff handleStuff = (stuff) -> alert('handling stuff'); 它汇编为: var Stuff; Stuff = (function() { var handleStuff; function Stuff() {} handleStuff = function(stuff) { return alert('handling

我从咖啡脚本开始。(还有英语,所以对于语法错误我很抱歉。)看这节课:

class Stuff
  handleStuff = (stuff) ->
    alert('handling stuff');
它汇编为:

var Stuff;
Stuff = (function() {
  var handleStuff;

  function Stuff() {}

  handleStuff = function(stuff) {
    return alert('handling stuff');
  };

  return Stuff;

})();
在Html上,我创建了一个Stuff实例,但该死的是它没有handleStuff方法。
为什么?

您希望
handleStuff
出现在原型上,因此将其更改为:

class Stuff
  handleStuff: (stuff) ->
    alert('handling stuff');
区别在于冒号与等号

其汇编目的是:

var Stuff;

Stuff = (function() {
  function Stuff() {}

  Stuff.prototype.handleStuff = function(stuff) {
    return alert('handling stuff');
  };

  return Stuff;

})();
您可以看到它在这里工作:


课堂材料
handleStuff:(东西)->
警惕(“处理物品”);
stuffInstance=newStuff()
stuffInstance.handleStuff()