Skip to main content

Posts

Showing posts with the label closures

why is "this” pointer scope in javascript restricted?

Scope or visibility of the this pointer which is called w.r.t a div is restricted to the function to which it was intended to and not to its NESTED functions. But,all the local variables of that function are visible in the nested functions -- why does this happen??.. getting the this pointer via calling the function with <function name>(this) on a div -- doesnt this mean even the this pointed also is a local variable in the <function name> In short, here's the code which confused me.. <html> <script type="text/javascript" > function tst1(){ setTimeout( function (){ alert(this.id) }, 2000) } function tst2(){ var elem = this setTimeout( function (){ alert(elem.id) }, 2000) } </script> <style type="text/css"> .test{ margin: 40px 100px; background-color: green; width: 200px; line-height: 40px; text-align: center; height: 40px; } </styl