+ 2
Delete a "definition"
If we have, for example, this code: function i(){ .... return "text";} var j=.. ; if(j==2) for (var i=0;i<....;i++) {....} document.write(i()); it will give an error message ("i" is not a function) because of the "i" inside the "for" loop. I don't want to modify the name of the function or the variable inside "for", I need to delete "i" which is inside "for" after finishing the loop (If the condition is met). another expression, how can I fix the error without changing names? is there a method? Or am I forced to modify every "i" inside the function or the loop?
5 ответов
+ 1
You can use "let" keyword in for loop, so replace var with let
for (let i=0;i<....,i++) {..}
+ 2
In first code you type in question, if you change var to let, it will work, because you call function outside loop
But here it wont because you call i function inside loop where something with same name exist.
You need to change name of i inside loop or name of your function, if you plan to use this inside loop.
This is how js works. It first check does this variable/function exist in current scope, in your case for loop, if exist it will try to run this code, if dont exist it will look to higher scope like function or global.
Also it is good practice to name function with some name what explain purpose of function.
+ 1
PanicS
Problem solved!
Thank you very much.
0
Ipang who asked that, not me, thanks anyway.