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

Discussion in 'Javascript' started by Jake Barnes, Feb 15, 2006.

  1. Jake Barnes

    Jake Barnes Guest

    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
    Jake Barnes, Feb 15, 2006
    #1
    1. Advertising

  2. Jake Barnes wrote:

    > 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
    Thomas 'PointedEars' Lahn, Feb 15, 2006
    #2
    1. Advertising

  3. Jake Barnes

    RobG Guest

    Re: if a variable is started inside a closure, can it ever be redeclaredand started over, outside of the original function?

    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
    >>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.


    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?


    --
    Rob
    RobG, Feb 15, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Parvis
    Replies:
    14
    Views:
    778
  2. Jason
    Replies:
    0
    Views:
    188
    Jason
    Jul 6, 2004
  3. Robert Klemme

    local variable and closure

    Robert Klemme, Jan 30, 2006, in forum: Ruby
    Replies:
    3
    Views:
    145
    Schüle Daniel
    Jan 30, 2006
  4. Julian Mehnle
    Replies:
    0
    Views:
    229
    Julian Mehnle
    Jul 17, 2003
  5. Oxy

    Control over IE Window Closure

    Oxy, Oct 31, 2006, in forum: Javascript
    Replies:
    4
    Views:
    73
Loading...

Share This Page