This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Human = new Class({ | |
initialize: function(name, age){ | |
this.name = name; | |
this.age = age; | |
}, | |
isAlive: true, | |
energy: 1, | |
eat: function(){ | |
this.energy++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Loop = new Class({ | |
loopCount: 0, | |
isStopped: true, | |
isLooping: false, | |
loopMethod: $empty, | |
setLoop: function(fn, delay){ | |
if(this.isLooping) { | |
this.stopLoop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tabs = new Class({ | |
Implements: [Options, Events], | |
options: { | |
nav: '.nav', | |
content: '.section', | |
currentClass: 'current' | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Person = new Class({ | |
Implements: Options, | |
options: { | |
height: 'tall', | |
weight: 'fat' | |
}, | |
jQuery: 'person', // must be after options definition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Human = new Class({ | |
initialize: function(name, age){ | |
this.name = name; | |
this.age = age; | |
}, | |
isAlive: true, | |
energy: 1, | |
eat: function(){ | |
this.energy++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Samurai = new Class({ | |
Extends: Human, | |
Implements: [Warrior], | |
side: 'good', | |
energy: 1000 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |