Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > if a variable is started inside a closure, can it ever be redeclared and started over, outside of the original function?

Reply
Thread Tools

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

 
 
Jake Barnes
Guest
Posts: n/a
 
      02-15-2006


In the function below, the reset() method has no effect. What is the
correct way to ensure that
singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox 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
singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox =
new Object();
var singletonCallBackAction = new Object();

objectForAccessingArrayOfItemsForCommunicationBox. set =
function(idOfElement, initialValue) {

singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox[idOfElement]
= initialValue;
}

objectForAccessingArrayOfItemsForCommunicationBox. get =
function() {
return
singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox;
}

objectForAccessingArrayOfItemsForCommunicationBox. reset =
function() {
var
singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox =
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

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-15-2006
Jake Barnes wrote:

> In the function below, the reset() method has no effect. What is the
> correct way to ensure that
> singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox 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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      02-15-2006
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
>>singletonArrayToHoldElementsForTheNextRenderingO fTheCommunicationBox 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 = objectForAccessingArrayOfItemsForCommunicationBox
nextObj =
singletonArrayToHoldElementsForTheNextRenderingOfT heCommunicationBox
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?


--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
CallForward outside->inside->outside failed after one ring. Yogz Cisco 1 04-16-2007 03:58 PM
forwarding "sections" of a conversation to someone on the outside (not in the original conversation) Joebrazeau@gmail.com Computer Support 5 08-19-2006 01:34 PM
inside-outside-inside issue on PIX 506E Dan Rice Cisco 9 02-04-2005 12:04 AM
declare variable inside or outside block? Angus Parvis Java 14 01-02-2005 06:32 PM



Advertisments