title: create a javascript function programmatically
body: <p>Need this for the youtube api // the onStateChange callback functions!</p>
  
  <p>I want to programmatically create functions which will listen to the "onStateChange" event emitted by several youtube player. Adding the listener works already:</p>
  
  <pre><code>function onYouTubePlayerReady(playerId) {
    var ytpStateManager = playerId +"_StateManager";
    document.getElementById(playerId).addEventListener("onStateChange", ytpStateManager );
  ...
  </code></pre>
  
  <p>The function I need to create based on the playerId variable ("ytp_1", "ytp_2", ...) is</p>
  
  <pre><code>function ytpStateManager(newState) {
    ytpStateHelper(playerId , newState);
  }
  </code></pre>
  
  <p>So the result for the playerId "ytp_1" would look like this:</p>
  
  <pre><code>function ytp_1_StateManager(newState) {
    ytpStateHelper("ytp_1", newState);
  }
  </code></pre>
  
  <p>Works also but right now I need to add them manually for each player, which is not what I need. I want to create them automatically when a new player sends a readyState event.</p>
  
  <p>My problem is that it seems like these functions need to be a global functions to work properly. I tried several options for days now. My problem is that I do not know how (if there is a way) to define a global function, incl. the function name, programmatically, based on another variable.</p>
  
  <p>Its a bummer that the ytp does not emit an event which includes the state AND the player/target. Would make things much easier. All this is basically the workaround as I need all to do stuff on all stateChanges.</p>
  
  <p>If there is a better/simpler way, PLEASE let me know :) Otherwise a solution for this question is highly welcome.</p>
  
  <p><strong>Maybe there is a way to rerout the event, to make it more "accessible"?</strong></p>
  
  <p>I read in the spec that .addEventListener also takes a object, so I tried to bind the event to a dedicated object. But again, it did not get triggered. Feels like I tested everything ...</p>
  
  <p><strong>UPDATE</strong>
  I am now switching to the iframe player (from swfobject) because that one provides an event which includes playerId and state :D Yeahhh!! After spending week with the wrong ytplayer this feels like a great advancement. Also seems like yt wants us to use the iframe player which can dynamically use html5 when supported.</p>