javascript - variable scope clarification -


I have a snippet of JavaScript code, which I have found in privacy "(A John Resig Book) I have a problem understanding the behavior of a variable. The following code (simplified in relation to the original one):

  (function () {var results; this .assert = function assert () {var li = document.createElement ("Li"); results.appendChild (s); return li;}; this.test = function test (name, fn) {results = document.getElementById ( "Result"); results = assert (). AppendChild (document.createElement) ("ul")); fn ();}}}} (); Window Myload  results  onload = function {test ("a test.", Function () {Enter}; emphasis ();});}   

/ Strong> variable. When you enter the "test" function, the result variable will assume the first "ul # results" and then "ul" value the append Child function. But when you enter the "fn ()" function, the value of the "result" is still the "ul # result". Why? I have some difficulty in understanding the scope of this variable

Can anyone help me understand this topic?

Thank you very much.

Variable has been created in the scope of anonymous function. same results variable Both and

Comments