JavaScript Binding
<p>thank you if you can help.  Source of code <a href="http://ejohn.org/apps/learn/#84" rel="nofollow">http://ejohn.org/apps/learn/#84</a></p>

<p>1) in line 3 of  the program below, where it says <code>return context[name]</code> what does this mean? Im guessing that it means <code>name</code> is bound to the <code>context</code> as a result of the <code>apply</code> function? Is that correct?</p>

<p>2)If my guess in 1 is correct,   why does it use the <code>[]</code> brackets? Is that just the syntax. When I look at it, it makes me think array or object? </p>

<p>3) When it says <code>apply(context, arguments)</code> is <code>arguments</code> not the same as <code>name</code> or is arguments both <code>context</code> and <code>name</code> together? to put it another way, in the language of the call <code>bind(Button, "click")</code> is <code>arguments</code> only <code>"click"</code> or is it both <code>button</code> and <code>click</code>?</p>

<p>4) I tried to rewrite line 3 by substituting name for arguments like this</p>

<pre><code>return context[name].apply(context, name); 
</code></pre>

<p>but it didn`t work anymore, which raises the questions</p>

<p>a)if it is returning name bound to context (i.e. context[name]), why isn`t it sufficient to just have apply(context,name)?</p>

<p>b) if <code>arguments</code> includes both <code>name</code> and <code>context</code>, is the third line of the function essentially </p>

<pre><code>return context[name].apply(context, [context, name]); 
</code></pre>

<p>c) if my assumption in 4(b) is correct, why would we effectively have to have context passed twice in order to bind name to context? which is to say, I don<code>t understand why line 3 doesn</code>t work if you just write  <code>apply(context, name)</code> instead of <code>apply(context,arguments)</code></p>

<pre><code>function bind(context, name){ 
  return function(){ 
    return context[name].apply(context, arguments); 
  }; 
} 

var Button = { 
  click: function(){ 
    this.clicked = true; 
  } 
}; 

var elem = document.createElement("li"); 
elem.innerHTML = "Click me!"; 
elem.onclick = bind(Button, "click"); 
document.getElementById("results").appendChild(elem); 

elem.onclick(); 
assert( Button.clicked, "The clicked property was correctly set on the object" );

Click me!
</code></pre>
