题
Zepto.js是否有一种添加类并使用子类扩展的方法?
一个相关的问题是:CoffeeScript是否可以使您能够编写课程并扩展它们的能力,而无需像原型这样的库,该原型具有特定的方法?
解决方案
zepto.js消息来源的浏览显示了它有一个 $.extend
可能起作用的方法,但它更多的是两个对象实现的合并,而不是传统的继承模型(它将提供超级访问者之类的东西。
CoffeeScript将生成为您提供可能/可能无法寻求的典型继承模型所需的代码。
在:
class Person
constructor: (@name) ->
class Ninja extends Person`
出去:
var Ninja, Person;
var __hasProp = Object.prototype.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;
};
Person = function() {
function Person(name) {
this.name = name;
}
return Person;
}();
Ninja = function() {
function Ninja() {
Ninja.__super__.constructor.apply(this, arguments);
}
__extends(Ninja, Person);
return Ninja;
}();
不隶属于 StackOverflow