noncom
noncom

Reputation: 4992

CoffeeScript - problems passing args to super constructor

I have the following CoffeeScript code:

planet = new Planet p5, {x: 100, y: 100, diameter: 20}

and somewhere else:

class GameObject
  constructor: (@p5, @x, @y) ->
    @selected = false

class Planet extends GameObject
  constructor: (p5, opts) ->
    super (p5 opts.x opts.y)
    @diameter = opts.diameter

and for the super line it says:

Uncaught TypeError: Property 'x' of object #< Object> is not a function

and it was ok when it was just:

class Planet
  constructor: (p5, opts) ->
    @x = opts.x
    @y = opts.y
    @diameter = opts.diameter
    @selected = false

i.e. before making it a child of a more generic GameObject... I have tried some rearrangements to make it work, but all invain. Not sure if it relates to CoffeeScript or JavaScript. The "try CoffeScript" thing on the official website spots no errors here. Browser is Chrome... What is wrong here and how do I overcome this?

Upvotes: 0

Views: 1354

Answers (2)

Niko
Niko

Reputation: 26730

You're missing commas to separate the arguments:

super (p5 opts.x opts.y)

should be

super (p5, opts.x, opts.y)

Otherwise, that line is interpreted as super(p5(opts.x(opts.y))), hence the "not a function" error.

Upvotes: 5

hvgotcodes
hvgotcodes

Reputation: 120198

don't you just want

super p5, opts.x, opts.y

Here is a link to your code running with no errors.

Upvotes: 2

Related Questions