CoffeeScript - 将参数传递给超级构造函数时出现问题

CoffeeScript - problems passing args to super constructor

本文关键字:构造函数 问题 参数传递 CoffeeScript      更新时间:2023-09-26

我有以下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行,它说:

未捕获的类型错误:对象 #<对象的属性"x"> 不是函数

当它只是时没关系:

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

即在使其成为更通用GameObject的孩子之前......我已经尝试了一些重新安排以使其工作,但都是徒劳的。不确定它是否与CoffeeScript或JavaScript有关。官方网站上的"尝试CoffeScript"在这里没有发现错误。浏览器是铬...这里出了什么问题,我该如何克服?

您缺少用于分隔参数的逗号:

super (p5 opts.x opts.y)

应该是

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

否则,该行被解释为super(p5(opts.x(opts.y))),因此"不是函数"错误。

你不就是想要吗

super p5, opts.x, opts.y

这是指向运行且没有错误的代码的链接。