Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript 'error: Invalid object key'

Tags:

coffeescript

I'm very new to coffeescript. So what does this error actually mean?

this is the class

class Animation
    constructor: (t) ->
        @startTime: t

I'm trying to set it up so that this class has a member startTime initialized to t during construction. Am I doing it wrong?

like image 452
FatalCatharsis Avatar asked Sep 26 '14 16:09

FatalCatharsis


1 Answers

Your code is creating an object in the constructor (and not saving the reference anywhere) with a key of @startTime. The error occurs because @ isn't a valid character for an object key.

Try this instead:

class Animation
    constructor: (@startTime) ->

Here's the generated JavaScript:

var Animation;

Animation = (function() {
  function Animation(startTime) {
    this.startTime = startTime;
  }

  return Animation;

})();

Here's where you can see the syntax for what you wanted to do: http://coffeescript.org/#classes

Here's the syntax you were incorrectly and unintentionally using: http://coffeescript.org/#literals (the section titled "Objects and Arrays")

like image 50
Daniel Kaplan Avatar answered Oct 28 '22 20:10

Daniel Kaplan