2012-06-10 61 views
0

我有以下的CoffeeScript代码:的CoffeeScript - 传递参数传递给超构造问题

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

和别的地方:

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

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

,为super线,它说:

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

,它只是:

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

即使它成为一个更通用的GameObject的孩子......我尝试了一些重新安排,使其工作,但所有入侵。不确定它是否与CoffeeScript或JavaScript相关。官方网站上的“尝试CoffeScript”的东西在这里没有错误。浏览器是铬...这里有什么问题,我如何克服这一点?

回答

5

你错过了逗号的分隔参数:

super (p5 opts.x opts.y) 

应该

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

否则,该行解释为super(p5(opts.x(opts.y))),因此 “不是一个函数” 的错误。