【发布时间】:2019-09-15 02:54:19
【问题描述】:
如何使用 CoffeeScript 在不同文件中正确组织子类?这是代码问题的简单示例。 Snake 运行得很好,但是尝试使用 Dog 类(因为它被放在另一个类中),它给出了以下错误:
TypeError: Dog 不是构造函数
主文件: .test/Animals.coffee
#expect = require "expect.js"
Animal = require "../learning/Animals"
Snake = Animal.Snake
Dog = require "../learning/Dog"
#Dog = Animal.Dog #unresolved variable
describe 'animals', ->
it 'test inheritance', ->
sam = new Snake "Sammy the Python"
peanut = new Dog "Peanut the Dog"
sam.move()
peanut.move()
父类: .learning/Animals.coffee
class Animal
constructor: (@name) ->
move: (meters) ->
console.log(@name + " moved #{meters}m.")
class Snake extends Animal
move: ->
console.log( "Slithering...")
super 5
module.exports = { Animal, Snake }
儿童班: .learning/Dog.coffee
Animal = require './Animals'
class Dog extends Animal
move: ->
console.log( "Runs...")
super 15
module.exports = { Dog }
【问题讨论】:
标签: javascript node.js coffeescript