Keen eyes

Steven D'Aprano steve at pearwood.info
Sun Jan 17 02:10:09 EST 2016


On Sun, 17 Jan 2016 10:25 am, jonas.thornvall at gmail.com wrote:

> double use of j in two different functions

Are you using a global variable called "j" as a loop variable? That sounds
like a terrible idea.

You should use local variables. Then a function with a local variable j
cannot possibly effect another function with a local variable also called
j.

Wait... is somebody going to tell me that Javascript defaults to global
variables inside functions?


js> function a(){
  > for (j=2;j<10;j++){}
  > return 1
  > }
js> function b(){
  > for (j=2; j<20;j++){}
  > return 1
  > }
js> j
js: "<stdin>", line 13: uncaught JavaScript runtime exception:
ReferenceError: "j" is not defined.
        at <stdin>:13

js> a()
1
js> j
10
js> b()
1
js> j
20



And this is the language that 95% of the Internet uses... my brain hurts.

-- 
Steven




More information about the Python-list mailing list