I've encountered a 'Cannot read property 'apply' of undefined' error when trying to define a class using CoffeeScript. As I'm new to both coffeeScript and JavaScript, I can't understand this error. Could somebody please help me.
Here is my coffeeScript code:
class Test
constructor: (@data) ->
@sums = @calculateSum()
console.log @sums
calculateSum: () =>
sums = 0
for i in [[email protected]] by 1
sums += @data[i]
return sums
window.Test = Test
Thanks.
@dataarray starting from1, so you are missing@data[0]in thesums.@sums = eval(@data.join('+'))would do very same as your@sums = @calculateSum()method. Btw, watch out forevalas this is risky to use it, but sometimes it gets quite handy.eval(@data.join('+')): this code would join all elentents of the array into a string while putting a '+' between them. Then we evaluate that string (run it as it would be a line in a JS file) to get result.