Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create an 'Animal' class by extending
- // the 'Object' class with our magic method
- var Animal = Object.extend(Object, {
- move : function() {alert('moving...');}
- });
- // Create a 'Dog' class that extends 'Animal'
- var Dog = Object.extend(Animal, {
- bark : function() {alert('woof');}
- });
- // Instantiate Lassie
- var lassie = new Dog();
- // She can move and bark!
- lassie.move();
- lassie.bark();
- // Create a namespace / module for your project
- window.MyModule = {};
- // Commence scope to prevent littering
- // the window object with unwanted variables
- (function() {
- var Animal = window.MyModule.Animal = Object.extend(Object, {
- move: function() {alert('moving...');}
- });
- // .. more code
- })();
- var Dog = Object.extend(Animal, {
- bark: function() {
- alert('woof');
- }
- // more methods ..
- }).implement(Mammal, Carnivore);
- // Instantiate object
- var lassie = new Animal('Lassie');
- // Register listener
- lassie.on('eat', function(food) {
- this.food += food;
- });
- // Feed lassie by triggering listener
- $('#feeding-button').click(function() {
- var food = prompt('How many food units should we give lassie?');
- lassie.trigger('eat', [food]);
- alert('Lassie has already eaten ' + lassie.food + ' units');
- });
- var Q = {};
- Q.test = function(){};
- if (!Q)
- var Q = {};
- Q.myFunction = function(){};
- if (Q.myFunction)
- Q.myFunction();
- else
- // Use a different approach/method
- var myInstance = {
- method1: function () {
- // ...
- },
- method2: function () {
- // ...
- }
- };
- $('#nav').click(function() {
- $(this).css('color','#f00').fadeOut();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement