• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in closures

 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David,
I am sorry for the wrong code.The correct code is below.



Similarly the other code.

Thanks .
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And under what circumstances are you saying it doesn't work?
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally think that with is an abomination. Your mileage may vary.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...
Thanks for the help.Hope it works for me too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic