Sha256: 1d12736f2df136fab5d66541a5b34dc5ec0c4863c1e4ebcb379db8dc483161be

Contents?: true

Size: 1.77 KB

Versions: 7

Compression:

Stored size: 1.77 KB

Contents

// base class
var Animal = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  name: "",
  eat: function() {
    return this.say("Yum!");
  },
  say: function(message) {
    return this.name + ": " + message;
  }
});

// subclass that augments a method
var Cat = Class.create(Animal, {
  eat: function($super, food) {
    if (food instanceof Mouse) return $super();
    else return this.say("Yuk! I only eat mice.");
  }
});

// empty subclass
var Mouse = Class.create(Animal, {});

//mixins
var Sellable = {
  getValue: function(pricePerKilo) {
    return this.weight * pricePerKilo;
  },

  inspect: function() {
    return '#<Sellable: #{weight}kg>'.interpolate(this);
  }
};

var Reproduceable = {
  reproduce: function(partner) {
    if (partner.constructor != this.constructor || partner.sex == this.sex)
      return null;
    var weight = this.weight / 10, sex = Math.random(1).round() ? 'male' : 'female';
    return new this.constructor('baby', weight, sex);
  }
};

// base class with mixin
var Plant = Class.create(Sellable, {
  initialize: function(name, weight) {
    this.name = name;
    this.weight = weight;
  },

  inspect: function() {
    return '#<Plant: #{name}>'.interpolate(this);
  }
});

// subclass with mixin
var Dog = Class.create(Animal, Reproduceable, {
  initialize: function($super, name, weight, sex) {
    this.weight = weight;
    this.sex = sex;
    $super(name);
  }
});

// subclass with mixins
var Ox = Class.create(Animal, Sellable, Reproduceable, {
  initialize: function($super, name, weight, sex) {
    this.weight = weight;
    this.sex = sex;
    $super(name);
  },

  eat: function(food) {
    if (food instanceof Plant)
      this.weight += food.weight;
  },

  inspect: function() {
    return '#<Ox: #{name}>'.interpolate(this);
  }
});

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
Fingertips-headless-squirrel-0.2.0 test/regression/prototype/unit/fixtures/class.js
Fingertips-headless-squirrel-0.3.0 test/regression/prototype/unit/fixtures/class.js
Fingertips-headless-squirrel-0.4.0 test/regression/prototype/unit/fixtures/class.js
Fingertips-headless-squirrel-0.5.0 test/regression/prototype/unit/fixtures/class.js
alloy-js-test-san-0.1.0 test/regression/prototype/unit/fixtures/class.js
alloy-js-test-san-0.1.1 test/regression/prototype/unit/fixtures/class.js
headless-squirrel-0.5.1 test/regression/prototype/unit/fixtures/class.js