473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Javascript Closure Question

13
Hello all,

I have been trying to understand how JavaScript closures work but I have been struggling. In this code snippet that I created to test things, I can't figure out how to change the value of the variable a to "b" without passing an argument into the function or returning the value. Can someone please help? Also, does anyone know of a site that explains closures in simple detail? Thanks.

Expand|Select|Wrap|Line Numbers
  1. function a() {
  2.     var a = "a";
  3.     blah = function() {
  4.         a = "b";
  5.     }
  6.     alert(a);
  7. }
  8.  
Oct 9 '07 #1
3 1223
dmjpro
2,476 2GB
Hello all,

I have been trying to understand how JavaScript closures work but I have been struggling. In this code snippet that I created to test things, I can't figure out how to change the value of the variable a to "b" without passing an argument into the function or returning the value. Can someone please help? Also, does anyone know of a site that explains closures in simple detail? Thanks.

Expand|Select|Wrap|Line Numbers
  1. function a() {
  2.     var a = "a";
  3.     blah = function() {
  4.         a = "b";
  5.     }
  6.     alert(a);
  7. }
  8.  
Look ...... two name coincides "function a" and "var a".
The main problem is not here.
Actually you can't change local references inside Closures :)
You have to declare it as "Global".

Debasis Jana
Oct 10 '07 #2
dhsieh
13
So this would effectively change the value of a to "b" globally?

Expand|Select|Wrap|Line Numbers
  1. var a = "a";
  2. function test() {
  3.     blah = function() {
  4.         a = "b";
  5.     }
  6.     alert(a);
  7. }
  8.  
Oct 10 '07 #3
dmjpro
2,476 2GB
So this would effectively change the value of a to "b" globally?

Expand|Select|Wrap|Line Numbers
  1. var a = "a";
  2. function test() {
  3.     blah = function() {
  4.         a = "b";
  5.     }
  6.     alert(a);
  7. }
  8.  
Call the function then see what happens.
Expand|Select|Wrap|Line Numbers
  1. alert(a);
  2. blah();
  3. alert(a)
  4.  
Debasis Jana
Oct 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
10
by: Emre Sevinc | last post by:
Take a look at the following snippet: <html> <head> <script> function add(elementId) { var container = document.getElementById(elementId); for (var i = 0; i < 10; i++) { var elt =...
10
by: lkagan2000 | last post by:
I'm reading 'Ajax in Action', (one of the best of the many tech books I've read) and there's an example that I just don't fully understand. If you happen to have the book, it's on page 75. I'm...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
6
by: scotty | last post by:
I have a script that loops through an existing table list and prepares each href element node to trigger a function when an image is clicked. The function that will be run passes a property value...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
10
by: Evan Charlton | last post by:
Hey all, I'm having some trouble with window.setInterval() within a custom object/prototype. Here is my code: function MyClass() { // do some junk // ... // define methods this.m_one =...
4
by: MartinRinehart | last post by:
I've written a short article explaining closures in JavaScript. It's at: http://www.martinrinehart.com/articles/javascript-closures.html I think I've understood. I look forward to your...
2
by: phpentrylevel | last post by:
hello i am a newbie to Javascript with a in depth background of PHP i have a simple script that replaces missing images when the webpage loads but i just cant get it to validate in JLint as my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.