if a variable is started inside a closure, can it ever be redeclared and started over, outside of th

J

Jake Barnes

In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox is
empty?


function getArrayOfElementsForCommunicationBox() {
// 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
storeReferencesToHTMLSafely
// 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 objectForAccessingArrayOfItemsForCommunicationBox = new
Object();
var
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox =
new Object();
var singletonCallBackAction = new Object();

objectForAccessingArrayOfItemsForCommunicationBox.set =
function(idOfElement, initialValue) {

singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox[idOfElement]
= initialValue;
}

objectForAccessingArrayOfItemsForCommunicationBox.get =
function() {
return
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox;
}

objectForAccessingArrayOfItemsForCommunicationBox.reset =
function() {
var
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox =
new Object();
}


objectForAccessingArrayOfItemsForCommunicationBox.setCallBack =
function(callBack) {
singletonCallBackAction["callBack"] = callBack;
}

objectForAccessingArrayOfItemsForCommunicationBox.getCallBack =
function() {
var callBack = singletonCallBackAction["callBack"];
return callBack;
}

return objectForAccessingArrayOfItemsForCommunicationBox;
}



var arrayOfElementsForCommunicationBox =
getArrayOfElementsForCommunicationBox();


arrayOfElementsForCommunicationBox.set("inputBox", "Please type your
address here.");
arrayOfElementsForCommunicationBox.reset(); // var still has values in
it, even after this
 
T

Thomas 'PointedEars' Lahn

Jake said:
In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox 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
 
R

RobG

Thomas said:
Jake Barnes wrote:




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 = objectForAccessingArrayOfItemsForCommunicationBox
nextObj =
singletonArrayToHoldElementsForTheNextRenderingOfTheCommunicationBox
cbArray = arrayOfElementsForCommunicationBox
*/

var dataObj = new Object();
var nextObj = new Object();
var singletonCallBackAction = new Object();

dataObj.set =
function(idOfElement, 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.setCallBack =
function(callBack) {
singletonCallBackAction["callBack"] = callBack;
}

dataObj.getCallBack =
function() {
var callBack = singletonCallBackAction["callBack"];
return callBack;
}

return dataObj;
}

// A Fn to help with debug
function showObjProps(obj)
{
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(cbArray.get()));

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

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

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

cbArray.reset(); // var still has values in it, even after this
alert('After reset\n' + showObjProps(cbArray.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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top