【问题标题】:Coffeescript, Classes & Zepto.jsCoffeescript、类和 Zepto.js
【发布时间】:2011-05-16 08:51:25
【问题描述】:

zepto.js 有添加类和扩展子类的方法吗?

一个相关的问题是:Coffeescript 是否实际上让您能够编写类并扩展它们,而无需像原型这样具有特定方法的库?

【问题讨论】:

    标签: javascript coffeescript


    【解决方案1】:

    浏览一下 Zepto.js 源代码表明它有一个 $.extend 方法可能有效,但它更多的是两个对象实现的合并,而不是传统的继承模型(它将提供像 Super 访问器之类的东西。)

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

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2011-12-07
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2012-05-31
      • 2012-10-14
      相关资源
      最近更新 更多