如何使用CoffeeScript在不同文件中正确组织子类?这是代码问题的简单示例。Snake运行得很好,但是随后尝试使用Dog类(因为将其放置在另一个类中),则产生以下错误:TypeError:Dog不是构造函数主文件: .test / Animals.coffee#expect = require "expect.js"Animal = require "../learning/Animals"Snake = Animal.SnakeDog = require "../learning/Dog"#Dog = Animal.Dog #unresolved variabledescribe 'animals', -> it 'test inheritance', -> sam = new Snake "Sammy the Python" peanut = new Dog "Peanut the Dog" sam.move() peanut.move()父班: .learning / Animals.coffeeclass Animal constructor: (@name) -> move: (meters) -> console.log(@name + " moved #{meters}m.")class Snake extends Animal move: -> console.log( "Slithering...") super 5module.exports = { Animal, Snake }子类别: .learning / Dog.coffeeAnimal = require './Animals'class Dog extends Animal move: -> console.log( "Runs...") super 15module.exports = { Dog }
添加回答
举报
0/150
提交
取消