在CoffeeScript中,使用class
定义类,使用关键字new
实例化对象。javascript
class Airplane takeOff: -> console.log 'Vrroom!' plane = new Airplane() plane.takeOff()
CoffeeScript中的类型系统基于JavaScript的原型继承实现。上面的代码生成的JavaScript代码为:java
var Airplane; Airplane = (function() { function Airplane() {} Airplane.prototype.takeOff = function() { return console.log('Vrroom!'); }; return Airplane; })();
上面的例子中,函数先声明空构造函数,并将takeOff函数绑定到类的原型上。dom
状态存储在绑定到对象的属性里。使用@运算符实现。ide
class Airplane describle: -> "A #{@color} plane" plane = new Airplane() plane.color = 'white' console.log plane.describle # 输出white
class Airplane takeOff: (eta) -> @taxi() console.log "#{s}..." for s in [eta..0] console.log 'Vrrroom' taxi: -> if Math.random() > 0.5 console.log 'Taxiing...'
@something
其实是this.something
的别名。咱们甚至@替换出现的this。能够将@做为this做为参数传递。函数
CoffeeScript提供::
运算符,以简单实现对象原型的访问。如:this
Date::getYearAE = -> monthOffset = if @getMonth() < 11 then 1 else 0 @getFullYear() - 1944 - monthOffset today = new Date() console.log "It is the year #{today.getYearAE()} AE" # 输出 It is the year 71 AE
上面使用::
运算符的CoffeeScript代码编译为prototype
Date.prototype.getYearAE = function() { var monthOffset; monthOffset = this.getMonth() < 11 ? 1 : 0; return this.getFullYear() - 1944 - monthOffset; };
能够看到::
使用原型来实现把函数绑定到类。code
只要加上名字为constructor
的方法。同时,若是在构造函数列表中使用@
命名标识符,则自动赋值给同名属性对象
class Train constructor: (@numCars, @type = 'diesel') -> @load = 0 @capacity = @numCars * 100
静态方法为绑定到类自己,在没有任何实例化的对象时,能够做为单独的引用使用。在CoffeeScript中,使用@开头的函数为静态函数。例:继承
同时,也可将静态属性绑定到类。
class Bicyle @WHEELS = 2 @frameSizeByHeight: (rideHeight) -> Math.floor rideHeight * 0.82 Bicyle.frameSizeByHeight 10 # 调用静态方法 console.log Bicyle.WHEELS # 输出 2
在CoffeeScript中,类定义使用extends
代表这个类继承自另外一个类。
class Automobile honk: -> console.log 'Beep!' class Truck extends Automobile honk: -> super() console.log 'Wee-oo wee-oo wee-oo'
同时,CoffeeScript继承也支持重载方法,只要在子类中从新定义与父类同名(参数不要求匹配)的方法便可。同时,在重载方法内部,能够使用super()
函数