我CoffeeScript
可以扩展节点的http.Server
类:
{Server} = require 'http'
class MyServer extends Server
foo: 'bar'
myserver = new MyServer
console.log myserver.foo # 'bar'
class MyServer2 extends MyServer
constructor: -> super()
myserver2 = new MyServer2
myserver.listen 3000
如果我正确理解了这篇文章,express
请扩展connect
它进而扩展http.Server
。但是下面有一些继承问题:
Express = require 'express'
class MyApp extends Express
foo: 'bar'
myapp = new MyApp
console.log myapp.foo # undefined
class MyApp2 extends MyApp
constructor: -> super()
myapp2 = new MyApp2
console.log myapp2 # {}
myapp2.listen 3000 # throws TypeError
当listen
被调用时,它会抛出以下错误,因为myapp2
它是一个空对象{}
并且没有该listen
方法:
TypeError: Object #<MyApp2> has no method 'listen'
如何express
以面向对象的方式使用CoffeeScript
?