Question

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 library like prototype that has specific methods to do so?

Was it helpful?

Solution

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;
}();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top