I have read several papers on CoffeeScript OOP. From them, do I understand correctly that using the @ sign in CoffeeScript (and this prefix in JavaScript):
- for variables: makes variables members of a class instance. Each instance has it’s own such variable (non-static variable)
- for “methods”: makes methods static, which is contrary to what it does with variables
I am a noob in JS and CS, sorry. Their philosophy is quite different from what I am used to.
UPDATE
Here are references on the info that I have read:
just search for static.
Inside a method,
@is JavaScript’sthisand refers to the current object; the current object depends on how the method is called, seecallandapplyfor ways to mess around with a method’s@(AKAthis); you can also use=>to bind a method to an object in CoffeeScript.If you say
@p = 11, that’s the same asthis.p = 11and makespavailable in that object.Inside a class definition,
@refers to the class itself. So this:defines a class method and you can say
C.m()to execute it.Consider this example:
That will give you this output in the console:
Demo: http://jsfiddle.net/ambiguous/DFcRt/
These might also be of interest: