Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
ryanflorence / Human.js
Created May 9, 2010 03:36
Abstract MooTools Class Basics
var Human = new Class({
initialize: function(name, age){
this.name = name;
this.age = age;
},
isAlive: true,
energy: 1,
eat: function(){
this.energy++;
}
var Loop = new Class({
loopCount: 0,
isStopped: true,
isLooping: false,
loopMethod: $empty,
setLoop: function(fn, delay){
if(this.isLooping) {
this.stopLoop();
@ryanflorence
ryanflorence / Tab.js
Created May 10, 2010 20:49
Sample Tab Class for use with moo4q
var Tabs = new Class({
Implements: [Options, Events],
options: {
nav: '.nav',
content: '.section',
currentClass: 'current'
},
var Person = new Class({
Implements: Options,
options: {
height: 'tall',
weight: 'fat'
},
jQuery: 'person', // must be after options definition
// regular javascript api
var bob = new Person('#dude',{ height: 'short' });
bob.dance('salsa'); // dances the salsa and returns bob
bob.awesome; // returns true
bob.awesome = false; // set the property to something else
// jQuery api
$('#bob').person({ height: 'short' }); // instantiate with options
$('#bob').person(); // returns the class instance since it's already been instantiated
// or in other words, returns and object like `bob` from above
var Human = new Class({
initialize: function(name, age){
this.name = name;
this.age = age;
},
isAlive: true,
energy: 1,
eat: function(){
this.energy++;
}
var Warrior = new Class({
energy: 100,
kills: 0,
attack: function(target){
if (target.energy < this.energy){
target.isAlive = false;
this.kills++;
}
this.energy = this.energy - 5;
}
var Ninja = new Class({
Extends: Human,
Implements: [Warrior],
initialize: function(side, name, age){
this.side = side;
this.parent(name, age);
}
});
var blackNinja = new Ninja('evil', 'Nin Tendo', 'unknown');
var Samurai = new Class({
Extends: Human,
Implements: [Warrior],
side: 'good',
energy: 1000
});
// assuming #jack is an element
// and our samurai class takes
// a jQuery selector for the first
// argument in the initialize method
// instantiate a class instance
$('#jack').samurai(); // new Samurai('#jack')
// call a method
$('#jack').samurai('eat') // jack.eat()
// get a property