这就是我使用 Coffee-Script 的方式。
class Model
constructor: (animal = "Model") ->
console.log animal;
class Cat extends Model
constructor: (animal = "Cat") ->
super animal
class Kitten extends Cat
constructor: (animal = "Kitten") ->
super animal
new Kitten()
// => Kitten
这是编译后的 JavaScript:
var Cat, Kitten, Model,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Model = (function() {
function Model(animal) {
if (animal == null) {
animal = "Model";
}
console.log(animal);
}
return Model;
})();
Cat = (function(_super) {
__extends(Cat, _super);
function Cat(animal) {
if (animal == null) {
animal = "Cat";
}
Cat.__super__.constructor.call(this, animal);
}
return Cat;
})(Model);
Kitten = (function(_super) {
__extends(Kitten, _super);
function Kitten(animal) {
if (animal == null) {
animal = "Kitten";
}
Kitten.__super__.constructor.call(this, animal);
}
return Kitten;
})(Cat);
new Kitten();
你可以在这里自己尝试