tadpole Posted December 22, 2010 Share Posted December 22, 2010 I am with the understanding that a var declared inside of the curly brackets is local.while looking at W3C examples as shown here, the local var "myWindow" seems to be carried over to another function.while there is not a global var myWindow.I do not see that the var "myWindow" has been declared in the function closeWin() Am i delusional.Please slap me up side the head.Or the declaration of "any" var is optional and the person who is doing the examples is not showing a solid scripting style.I would really like to know, is this example showing a undeclared var myWindow in the function closeWin and is that legal to fly.Thank you. <script type="text/javascript">function openWin(){myWindow=window.open("","","width=200,height=100");myWindow.document.write("<p>This is 'myWindow'</p>");}function closeWin(){myWindow.close();}</script> Link to comment Share on other sites More sharing options...
jeffman Posted December 22, 2010 Share Posted December 22, 2010 Inside a function/method, defining a variable without the var keyword automatically declares it as global. It's legal (it works, right) but it is bad practice, since it can make reading the code difficult, especially for another coder or even for you after some time has passed.I am sure the example is written as it is to simplify matters. Link to comment Share on other sites More sharing options...
tadpole Posted December 22, 2010 Author Share Posted December 22, 2010 Thank youconfusion seems to be the missing lesson plan.here is a saw, tape measure and some wood, and thats a dog, good luck. Inside a function/method, defining a variable without the var keyword automatically declares it as global. It's legal (it works, right) but it is bad practice, since it can make reading the code difficult, especially for another coder or even for you after some time has passed.I am sure the example is written as it is to simplify matters. Link to comment Share on other sites More sharing options...
ApocalypeX Posted December 25, 2010 Share Posted December 25, 2010 Inside a function/method, defining a variable without the var keyword automatically declares it as global. It's legal (it works, right) but it is bad practice, since it can make reading the code difficult, especially for another coder or even for you after some time has passed.I am sure the example is written as it is to simplify matters.I've completely stopped using the var keyword except for special purposes.The var is a lie! Link to comment Share on other sites More sharing options...
jeffman Posted December 25, 2010 Share Posted December 25, 2010 @ApocalypeXYou might have mentioned, as you have in other threads, that this only works because you use namespaces, apparently more than I had imagined. But, yikes, you sound almost cultish about it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.