473,699 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if a variable is started inside a closure, can it ever be redeclared and started over, outside of the original function?



In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox is
empty?
function getArrayOfEleme ntsForCommunica tionBox() {
// 02-12-06 - I'm thinking it would be nice to have a global
object that contains two
// arrays, plus get and set methods for those arrays. Functions
like setText() can use
// these methods to specify what the dialog box should look like,
and then setText()
// can call askForInput() which will show the dialog box. These
two arrays, below
// specify what HTML elements should be placed in the
communication box.
//
// 02-13-06 - PLEASE NOTE - this function is different from
storeReferences ToHTMLSafely
// in that manages references to HTML blocks whereas this carries
strings of ids and
// initial text that should fill in the communication box form.

var objectForAccess ingArrayOfItems ForCommunicatio nBox = new
Object();
var
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox =
new Object();
var singletonCallBa ckAction = new Object();

objectForAccess ingArrayOfItems ForCommunicatio nBox.set =
function(idOfEl ement, initialValue) {

singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox[idOfElement]
= initialValue;
}

objectForAccess ingArrayOfItems ForCommunicatio nBox.get =
function() {
return
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox;
}

objectForAccess ingArrayOfItems ForCommunicatio nBox.reset =
function() {
var
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox =
new Object();
}
objectForAccess ingArrayOfItems ForCommunicatio nBox.setCallBac k =
function(callBa ck) {
singletonCallBa ckAction["callBack"] = callBack;
}

objectForAccess ingArrayOfItems ForCommunicatio nBox.getCallBac k =
function() {
var callBack = singletonCallBa ckAction["callBack"];
return callBack;
}

return objectForAccess ingArrayOfItems ForCommunicatio nBox;
}

var arrayOfElements ForCommunicatio nBox =
getArrayOfEleme ntsForCommunica tionBox();
arrayOfElements ForCommunicatio nBox.set("input Box", "Please type your
address here.");
arrayOfElements ForCommunicatio nBox.reset(); // var still has values in
it, even after this

Feb 15 '06 #1
2 1104
Jake Barnes wrote:
In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox is
empty?


I do not know yet. Maybe it is just me or because it is late but I do think
you should post easily _legible_ source code if you wish that question to
be answered. You could use shorter identifiers for a start.
PointedEars
Feb 15 '06 #2
Thomas 'PointedEars' Lahn wrote:
Jake Barnes wrote:

In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArra yToHoldElements ForTheNextRende ringOfTheCommun icationBox is
empty?

I do not know yet. Maybe it is just me or because it is late but I do think
you should post easily _legible_ source code if you wish that question to
be answered. You could use shorter identifiers for a start.


Hey, it must be late for you or you are mellowing! :-)

Here is something that might be more legible:

<script type="text/javascript">

function getcbArray()
{

/*
re-named variables:
dataObj = objectForAccess ingArrayOfItems ForCommunicatio nBox
nextObj =
singletonArrayT oHoldElementsFo rTheNextRenderi ngOfTheCommunic ationBox
cbArray = arrayOfElements ForCommunicatio nBox
*/

var dataObj = new Object();
var nextObj = new Object();
var singletonCallBa ckAction = new Object();

dataObj.set =
function(idOfEl ement, initialValue) {
nextObj[idOfElement] = initialValue;
}

dataObj.get =
function() {return nextObj;}

dataObj.reset =
function() {

// Here is the 'faulty' line
// var nextObj = new Object();

// Fixed line (delete 'var' keyword)
nextObj = new Object();

}

dataObj.setCall Back =
function(callBa ck) {
singletonCallBa ckAction["callBack"] = callBack;
}

dataObj.getCall Back =
function() {
var callBack = singletonCallBa ckAction["callBack"];
return callBack;
}

return dataObj;
}

// A Fn to help with debug
function showObjProps(ob j)
{
var txt = '';
for (var p in obj){txt += p+': '+obj[p]+'\n';}
return txt;
}

// Now a small test

var cbArray = getcbArray();
// Show nextObj's properties
alert('Before set\n' + showObjProps(cb Array.get()));

cbArray.set("in putBox", "Please type your address here.");
alert('After set\n' + showObjProps(cb Array.get()));

cbArray.reset() ; // var still has values in it, even after this
alert('After reset\n' + showObjProps(cb Array.get()));

// Make sure nextObj is still usable
cbArray.set("se cond", "blah blah blah.");
alert('After set\n' + showObjProps(cb Array.get()));

cbArray.reset() ; // var still has values in it, even after this
alert('After reset\n' + showObjProps(cb Array.get()));

</script>
The faulty line creates a new object called 'nextObj' within the scope
of the function. What should happen is that the outer function's
nextObj should be re-set, so replacing the faulty line with:

nextObj = new Object();
fixes that. Is it better to set the object to null or delete it first?
--
Rob
Feb 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
6488
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
34
2789
by: Volker Hetzer | last post by:
Hi! I've done lots of programming for CAD, which was basically C/C++ and tcl/tk. Now, we are thinking about introducing more web based tools, programming them ourselves and right now the toolchain we think about is apache/oracle/php. Now, I can do oracle no problem but I'm pretty wet behind the ears about everything else. What books could you recommend to me so that I can learn: - what all this apache stuff is about, the mod_* - what...
23
19189
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
28
4317
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
11
3025
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()
10
1871
by: Phillip Vong | last post by:
Newbie learning in VB.NET 2. I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link and I want to use this querystring over and over again through out the page. Example. Public Variable Dim myVar as string = Request.querystring("MyVar") End Public Variable
10
12793
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 involving variable scope in JavaScript.) I have a lot of code that generates HTML on the fly. This code has tags with id attributes derived from variables. A small example: blah('<span id="' + dev + '_' + mod + '">...</span>');
0
35236
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
9
1994
by: Erwin Moller | last post by:
Hi all, Is it possible (PHP5.2) to find the name of a variable used in the caller of a function from within the function itself? Or to be more clear: ...php code.. $result = foo($abc); ...more phpcode..
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.