In JavaScript I’d set up a class like:
var SomeClass = (function()
{
var _this = { };
var privateVar = 'foo'
_this.publicVar = 'bar'
function privateFunction()
{
return privateVar;
}
_this.publicFunction = function()
{
return _this.publicVar;
}
return _this;
})();
This is fine as in privateFunction I can reference publicFunction by either calling SomeClass.publicFunction() or _this.publicFunction()
Now in Coffeescript I’m trying to set up my class so that I can call class functions, how do I go about this? How can I create a class in coffeescript called Foo and get a function in the class to reference another function in that class?
Your question is confusing.
If you want to create a class method, you could do it like
note the last line. A class method is one that lives on the function that defines the class.
If you want to call a method from another method, its no big deal.
m1andm2in this example demonstrate this. Note that each invocation is scoped tothis, which is the current instance of your class Foo. So you could dowhich would create a new instance of Foo, and then invoke
m2on that instance.m2in turn invokes the class methodclassMethod.Check this out to see the javascript to which the above coffeescript compiles.