开发者

Coffeescript, Classes & Zepto.js

开发者 https://www.devze.com 2023-01-26 22:25 出处:网络
Does zepto.js have a method for adding classes and extending with subclasses? A connected question is: does Coffeescript give you, in effect, the ability to write classes and extend them without need

Does zepto.js have a method for adding classes and extending with subclasses?

A connected question is: does Coffeescript give you, in effect, the ability to write classes and extend them without needing a libra开发者_如何学Gory like prototype that has specific methods to do so?


A skim of the Zepto.js source shows it has an $.extend method which may work, but it's more of a merging of two objects implementation than a traditional inheritance model (which would provide things like Super accessors.)

CoffeeScript will generate the code required to give you the typical inheritance model you may/may not seek.

in:

class Person
    constructor: (@name) ->

class Ninja extends Person`

out:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号