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