Advertisement
wingman007

Events_step1_JS

Jul 3rd, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(){
  2.     console.log('Hello. Step 1. The simplest possible variant. A function calls another function - event listener.');
  3.     // I will be creating events
  4.     // 1) Only 1 class. The function trigger and the event listener are in the same class.
  5.     // Everyhtign is hard coded listeners can not be called at runtime in C# for example in JS we will not have problems.
  6.     // Even in C# we can inherit and override the function "onExecuteSomething"
  7.     function A()
  8.     {
  9.         this.executeSomething = function(){ // the trigger
  10.             console.log('I have to execute something and call the event listener afterwords');
  11.             this.onExecuteSomething();
  12.             // if you want to trigger another listener you have to add extra code here
  13.             // ... this.anotherOnExecuteSomethign
  14.         };
  15.        
  16.         // In the very same class I can define an event listener
  17.         // Event listener
  18.         this.onExecuteSomething = function(){ // the listener
  19.             console.log('I have been called by executeSomething/foo/bar/bas');
  20.         }
  21.     }
  22.    
  23.     // Instantiate an object call the method(s)
  24.     var a = new A();
  25.     a.executeSomething();
  26.    
  27. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement