I try to set a default value for a given class property.
class MyClass
name: (name = 'value') ->
The code above doesn’t return value. It returns the whole function.
a = new MyClass
a.name
> function (name) {
if (name == null) name = 'value';
}
If I set a value to it then it works:
a = new MyClass
a.name = 'something'
a.name
> 'something'
I’m wondering what would be the proper way to set a default value for a Class property in CoffeScript.
This:
simply defines
nameas a method which takes one parameter, also calledname, and the default value for that parameter is'value'. So, if we add a body and execute the method:You’ll see
'value'and'pancakes'in the console. Here’s a demo, open your console to see what happens.If you want instances of
MyClassto have a name property whose default value is'value'then you want to say this:That will also give you
'value'and'pancakes'in the console.