hook.js Test


hook.js Code

var Hook = {
  hooks: [],

  register: function ( name, callback ) {
    if( 'undefined' == typeof( Hook.hooks[name] ) )
      Hook.hooks[name] = []
    Hook.hooks[name].push( callback )
  },

  call: function ( name, arguments ) {
    if( 'undefined' != typeof( Hook.hooks[name] ) )
      for( i = 0; i < Hook.hooks[name].length; ++i )
        if( true != Hook.hooks[name][i]( arguments ) ) { break; }
  }
}

test() Code

function test () {
  el = document.getElementById( 'test-area' );
  // Set up the hooks
  Hook.register( 'one', function ( args ) { el.innerHTML = el.innerHTML + 'One<br/>'; return true; } );
  Hook.register( 'one', function ( args ) { el.innerHTML = el.innerHTML + 'Two<br/>'; return true; } );
  Hook.register( 'one', function ( args ) { el.innerHTML = el.innerHTML + args[0]; args[0] = 'Six<br/>'; return true; } );
  Hook.register( 'one', function ( args ) { el.innerHTML = el.innerHTML + 'Four<br/>'; return false; } );
  Hook.register( 'one', function ( args ) { el.innerHTML = el.innerHTML + 'Five<br/>'; return true; } );

  // Set up the arguments
  arguments = [ 'Three<br/>' ];
  // Call the hooks
  Hook.call( 'one', [ 'Three<br/>' ] );
  // Prove the arguments were changed
  el.innerHTML = el.innerHTML + arguments[0];
}

Expected

One
Two
Three
Four
Six

Result


Run Test