[Logo]
Forums Register Login
problem in closures
Hi,
I am totally new to closures in javascript.I read a piece of code regarding closures but its not working.
The code is :


The output should be -> name:TEST
But I am getting -> name:undefined

The below code is not working either :



message in alert box is -> name:undefined

The above code works after the following changes :



message in alert box is -> name:TEST

Also when are closures created? Just by using inner functions or when we make inner functions accessible outside the scope of the function,containing the inner function.
Thanks
These all work fine for me, and should work fine for you: if they're not, be more specific with regards to how you're testing them.
Thanks David,
I am sorry for the wrong code.The correct code is below.



Similarly the other code.

Thanks .
And under what circumstances are you saying it doesn't work?
All I am saying is that it doesn't work if I am using the with statement.
Otherwise its working.
I am calling the test function on button click.
I personally think that with is an abomination. Your mileage may vary.
 
Max White wrote:All I am saying is that it doesn't work if I am using the with statement.

Does for me, and don't know why it wouldn't unless your browser is broken or you don't have an element named formName, which I assume you do, otherwise you'd have noticed when you tried to debug it that you're getting a console message. But you didn't mention it when I asked you to be specific, so I assume your JavaScript matches up with your DOM.

And I agree--there's no good argument for using with.
Hmmm...
Thanks for the help.Hope it works for me too
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 25, 2017 04:36:10.