Reputation: 30015
I've been using coffeescript on the front end for a few years now. And am familiar with class syntax looking something like this:
class MyClass
methodOne : ->
console.log "methodOne Called"
methodTwo : (arg, arrg) ->
console.log "methodTwo Called"
Recently I've been playing with node and the frappe boilerplate for web applications with coffeescript and node.
This script using CoffeeScript classes for routes with the following syntax:
class MyClass
@methodOne = ->
console.log "methodOne Called"
@methodTwo = (arg, arrg) ->
console.log "methodTwo Called"
The only usage difference I can note from my normal usage, is that the Routes.coffee file consumes the class directly rather than making a new
object. So:
MyClass.methodOne()
# vs
new MyClass().methodOne()
Now I've learned that the @methodOne
syntax is not using .prototype
while the other syntax does. But why would this make the usage fail?
Upvotes: 1
Views: 119
Reputation: 48147
So, methods that begin with @
are class methods where everything else is an instance methods. With instances methods, :
essentially means public where =
means private. The "public" vs "private" dichotomy doesn't exist with class methods in CoffeeScript, so :
and =
do the ame thing. They are both public.
For example, take a look at this class:
class MyClass
@methodOne = ->
@methodTwo : ->
methodThree : ->
methodFour = ->
That evaluates to the following JavaScript:
var MyClass;
MyClass = (function() {
var methodFour;
function MyClass() {}
MyClass.methodOne = function() {};
MyClass.methodTwo = function() {};
MyClass.prototype.methodThree = function() {};
methodFour = function() {};
return MyClass;
})();
So, methodOne
and methodTwo
are both public class methods, methodThree
gets to go on the prototype, so it is a public instance method and methodFour
becomes a variable within the class that can be used internally but never gets exposed publically.
Hope that answers what you are asking?
Upvotes: 2