質問

Zepto.jsには、クラスを追加してサブクラスで拡張する方法がありますか?

接続された質問は、Coffeescriptが実際にクラスを作成して、特定の方法を持つプロトタイプのようなライブラリを必要とせずに拡張する機能を提供しますか?

役に立ちましたか?

解決

zepto.jsソースのスキムはそれがあることを示しています $.extend 機能する可能性のある方法ですが、従来の継承モデル(スーパーアクセサのようなものを提供する)よりも2つのオブジェクトの実装のマージのようなものです。

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;
}();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top