2010-11-26 50 views
1

zepto.js是否有方法來添加類並使用子類進行擴展?Coffeescript,Classes&Zepto.js

一個相關的問題是:Coffeescript實際上給你寫類和擴展它們的能力,而不需要像具有特定方法這樣做的原型庫嗎?

回答

3

的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; 
}();