Coffeescript类语法不同节点?

现在我已经在前端使用了coffeescript几年了。 熟悉类语法看起来像这样:

class MyClass methodOne : -> console.log "methodOne Called" methodTwo : (arg, arrg) -> console.log "methodTwo Called" 

最近我一直在玩节点和frappe样板的web应用程序与咖啡和节点。

此脚本使用以下语法的路由使用CoffeeScript类:

  class MyClass @methodOne = -> console.log "methodOne Called" @methodTwo = (arg, arrg) -> console.log "methodTwo Called" 

我可以从我的正常使用中注意到唯一的用法差异是Routes.coffee文件直接消耗类,而不是创build一个new对象。 所以:

  MyClass.methodOne() # vs new MyClass().methodOne() 

现在我已经了解到, @methodOne语法不使用.prototype而另一种语法则使用。 但是,为什么这会导致使用失败?

因此,以@开头的方法是其他所有方法都是实例方法的类方法。 有了实例方法, :本质上意味着public = where是私有的。 在CoffeeScript中,“public”与“private”二分法并不存在类方法,所以:=做一些事情。 他们都是公开的。

例如,看看这个类:

 class MyClass @methodOne = -> @methodTwo : -> methodThree : -> methodFour = -> 

评估到以下JavaScript:

 var MyClass; MyClass = (function() { var methodFour; function MyClass() {} MyClass.methodOne = function() {}; MyClass.methodTwo = function() {}; MyClass.prototype.methodThree = function() {}; methodFour = function() {}; return MyClass; })(); 

所以, methodTwomethodTwo都是公共类方法, methodThree是进入原型,所以它是一个公有的实例方法和方法,成为一个可以在内部使用但不会被公开暴露的类中的variables。

希望能回答你的问题?

    Interesting Posts