JSFiddle - React, Tailwind, and code Playground
by rgthree
JavaScript
var Organism = new Class({
Implements: [Events]
});
var Animal = new Class({
Extends: Organism,
speak: function(){
this.fireEvent('speak');
}
});
var Dog = new Class({
Extends: Animal,
sound: 'bark',
speak: function(){
console.log(' '+this.sound+'!');
this.parent();
},
scold: function(){
console.log(' _wimper_');
}
});
var Chihuahua = new Class({
Extends: Dog,
sound: 'arf',
scold: function(){
console.log(' _scurry_');
}
});
var Monitor = new Class({
initialize: function(animal){
this.animal = animal;
this.animal.addEvent('speak', this.hush.bind(this));
},
hush: function(){
console.log(' > hush!');
this.animal.scold();
}
});
console.clear();
console.log('---');
console.info('[dog] - expect "bark!" & "_wimper_":');
var dog = new Dog();
new Monitor(dog);
dog.speak();
console.info('[chihuahua] - expect "arf!" & "_scurry_":');
var chihuahua = new Chihuahua();
new Monitor(chihuahua);
chihuahua.speak();
console.info('[something (Dog -> Chihuahua :: __proto__)] - expect "arf!" & "_scurry_":');
var something2 = new Dog();
new Monitor(something2);
something2.__proto__ = Chihuahua.prototype;
something2.speak();
console.error('[something (Dog -> Chihuahua :: Object.create)] - expect "arf!" & "_scurry_":');
var something = new Dog();
new Monitor(something);
var propertiesHash = {};
Object.each( something, function( itm, idx ) {
propertiesHash[idx] = Object.getOwnPropertyDescriptor( something, idx );
});
something = Object.create( Chihuahua.prototype, propertiesHash );
something.speak();