1. #1
    Ext User
    Join Date
    Aug 2007
    Posts
    2
    Vote Rating
    0
    Wylde is on a distinguished road

      0  

    Default Ext.namespace

    Ext.namespace


    Code:
    1.  namespace : function(){
    2.    var a=arguments, o=null, i, j, d, rt;
    3.    for (i=0; i<a.length; ++i) {
    4.        d=a[i].split(".");
    5.        rt = d[0];
    6.        eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
    7.        for (j=1; j<d.length; ++j) {
    8.            o[d[j]]=o[d[j]] || {};
    9.            o=o[d[j]];
    10.       }
    11.   }
    12. },
    In this function, why in line 9 the var 'o' is reassigned with o[d[j]] ? if you remove line 9 ,nothing change in the normal workflow.

    Suppose Ext.namespace("Ext","Ext.utils");

    in the 1st cicle, only the first for it's executed, so o = Ext object.
    in the 2nd cicle, the 2nd for it's executed, so o["utils"] = {} ....ok
    but in the next line ... o = o["utils"] ?? if so, Ext object it's totally erased... or not ?


    thx for reading
    Last edited by mystix; 7 Aug 2007 at 7:03 AM. Reason: moved to Help from General

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    Since it is used pretty heavily, I would say it is highly unlikely it will blow away the Ext object.
    Jack Slocum
    Ext JS Founder
    Original author of Ext JS 1, 2 & 3.
    Twitter: @jackslocum
    jack@extjs.com

  3. #3
    Ext Premium Member
    Join Date
    Mar 2007
    Posts
    76
    Vote Rating
    0
    rtannert2 is on a distinguished road

      0  

    Default


    In line 9 o is set equal to the namespace that was just verified to exist or created if it didn't exist, to serve as the starting point for the next iteration of the inner loop. In the case of 'Ext.util' the inner loop is executed only once so line 9 doesn't really do anything helpful. Let's take a slightly more complicated example:

    Code:
    Ext.namespace('Ext.util.Format');
    In this example the inner loop will get executed twice. In the first iteration:

    Code:
        for (i=0; i<a.length; ++i) {        // evaluate 'Ext.util.Format'
            d=a[i].split(".");              // d = ['Ext', 'util', 'Format']
            rt = d[0];                      // rt = 'Ext'
            eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
                                            // Ext is created only if it doesn't exist, then o is set = Ext
            for (j=1; j<d.length; ++j) {    // first pass of inner loop: o[d[j]] = Ext['util']
                o[d[j]]=o[d[j]] || {};      // Ext['util'] is created if it doesn't exist
                o=o[d[j]];                  // o is set = Ext['util'], which is the same as Ext.util
            }
        }

    Then in the second iteration:

    Code:
            for (j=1; j<d.length; ++j) {    // second pass of inner loop: o[d[j]] = Ext.util['Format']
                o[d[j]]=o[d[j]] || {};      // Ext.util['Format'] is created if it doesn't exist
                o=o[d[j]];                  // o is set = Ext.util['Format'] in case another iteration is needed
            }
    Make sense?

  4. #4
    Ext User
    Join Date
    Aug 2007
    Posts
    2
    Vote Rating
    0
    Wylde is on a distinguished road

      0  

    Default


    thanks! now I 've understood what was wrong !

    in the famous line 9 ( o=o[d[j]]; ) o is simply referred (like a sort of pointer) to o[d[j]] !!
    i was convinced that o became EQUAL TO a VALUE (in the case of the first cicle a null object {})

    i'm saying right or ?

    anyway , thanks a lot for the time spent for me!! you have been very illuminant !!

  5. #5
    Ext Premium Member
    Join Date
    Mar 2007
    Posts
    76
    Vote Rating
    0
    rtannert2 is on a distinguished road

      0  

    Default


    Yes, you're correct. o is a pointer, or reference, to an object. Glad I could help!

Thread Participants: 2