I'm trying to write a program that takes a number and validates it according to the Luhn formula.
How do I set object properties in the class in CoffeeScript? I want to do this:
class Luhn
constructor: (@number) ->
lastDigit: @number % 10
But this produces:
function Luhn(number) {
this.number = number;
}
Luhn.prototype.checkDigit = Luhn.number % 10;
Which returns undefined, because it's trying to access a class variable.
When setting the prototype, you can do this, though:
function Luhn(number) {
this.number = number;
}
Luhn.prototype.lastDigit = function() {
return this.number % 10
}
Now an instance of the Luhn class has a function lastDigit() which works.
Is there a way of setting object properties in the class in CoffeeScript? I know I can set it in the constructor, like this:
class Luhn
constructor: (@number) ->
@lastDigit: @number % 10
But I want to set other, more complicated properties, and I don't want my constructor to become a mess. This is an idea, but it still kinda sucks:
class Luhn
constructor: (@number) ->
@lastDigit: @number % 10
@complicatedProperty1 = getComplicatedProperty1(@number)
@complicatedProperty2 = getComplicatedProperty2(@number)
What's the best way to go about this?