Jump to content

variable local value


tadpole

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...