Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. // Create an 'Animal' class by extending
  2. // the 'Object' class with our magic method
  3. var Animal = Object.extend(Object, {
  4. move : function() {alert('moving...');}
  5. });
  6.  
  7. // Create a 'Dog' class that extends 'Animal'
  8. var Dog = Object.extend(Animal, {
  9. bark : function() {alert('woof');}
  10. });
  11.  
  12. // Instantiate Lassie
  13. var lassie = new Dog();
  14.  
  15. // She can move and bark!
  16. lassie.move();
  17. lassie.bark();
  18.  
  19. // Create a namespace / module for your project
  20. window.MyModule = {};
  21.  
  22. // Commence scope to prevent littering
  23. // the window object with unwanted variables
  24. (function() {
  25.  
  26. var Animal = window.MyModule.Animal = Object.extend(Object, {
  27. move: function() {alert('moving...');}
  28. });
  29.  
  30. // .. more code
  31.  
  32. })();
  33.  
  34. var Dog = Object.extend(Animal, {
  35. bark: function() {
  36. alert('woof');
  37. }
  38. // more methods ..
  39. }).implement(Mammal, Carnivore);
  40.  
  41. // Instantiate object
  42. var lassie = new Animal('Lassie');
  43.  
  44. // Register listener
  45. lassie.on('eat', function(food) {
  46. this.food += food;
  47. });
  48.  
  49. // Feed lassie by triggering listener
  50. $('#feeding-button').click(function() {
  51. var food = prompt('How many food units should we give lassie?');
  52. lassie.trigger('eat', [food]);
  53. alert('Lassie has already eaten ' + lassie.food + ' units');
  54. });
  55.  
  56. var Q = {};
  57.  
  58. Q.test = function(){};
  59.  
  60. if (!Q)
  61. var Q = {};
  62.  
  63. Q.myFunction = function(){};
  64.  
  65. if (Q.myFunction)
  66. Q.myFunction();
  67. else
  68. // Use a different approach/method
  69.  
  70. var myInstance = {
  71. method1: function () {
  72. // ...
  73. },
  74. method2: function () {
  75. // ...
  76. }
  77. };
  78.  
  79. $('#nav').click(function() {
  80. $(this).css('color','#f00').fadeOut();
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement