javascript array is not empty on creation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rusty

    javascript array is not empty on creation

    Hi,

    im currently working on a web app which uses heavy javascript. in one
    of the functions, a simple array is created using "var admin_types =
    new Array();". This array is not empty, it has a length of 0 but
    contains one element with the name "clone" and the value

    function () { var copy = {}; for (var i in this) { var value = this[i];
    try { if (value != null && typeof (value) == "object" && value !=
    window && !value.nodeType ) { value.clone = Object.clone; copy[i] =
    value.clone(); } else { copy[i] = value; } } catch (e) { copy[i] =
    value; } } return copy; }

    why does this element get created?? The weird thing is that i use a lot
    of arrays and this is the only one that has that element.

    i tried this in firefox 1.0, ie 5.5 and mozilla 1.7.1 and the element
    is in the array for all of them...
    any help would be appreciated


    Rusty

  • rusty

    #2
    Re: javascript array is not empty on creation

    i just discovered that all of the arrays in the web app have the
    "clone" element. When i created a simple html page with a javascript
    function in the head that creates an array, it does not contain a
    "clone" element...

    Comment

    • Lee

      #3
      Re: javascript array is not empty on creation

      rusty said:[color=blue]
      >
      >i just discovered that all of the arrays in the web app have the
      >"clone" element. When i created a simple html page with a javascript
      >function in the head that creates an array, it does not contain a
      >"clone" element...[/color]

      It's not an element of the array, it's a method of the Array object.

      Comment

      • rusty

        #4
        Re: javascript array is not empty on creation

        if its a method, why can u access it using admin_types['clone'] ??

        Comment

        • RobG

          #5
          Re: javascript array is not empty on creation

          rusty wrote:[color=blue]
          > if its a method, why can u access it using admin_types['clone'] ??
          >[/color]

          admin_types['clone']

          is the same as:

          admin_types.clo ne

          Have a look here:

          <URL:http://www.faqts.com/knowledge_base/index.phtml/fid/144>


          --
          Rob

          Comment

          • rusty

            #6
            Re: javascript array is not empty on creation

            ok, thanks for the link, it helped a lot!

            i found the spot where the clone method is added to the prototype of
            Object, in a new library i recently included...

            thanks for the help but i still don't understand why u can access a
            method in the same way u can an element of an array??

            Comment

            • rusty

              #7
              Re: javascript array is not empty on creation

              ok, thanks for the link, it helped a lot!

              i found the spot where the clone method is added to the prototype of
              Object, in a new library i recently included...

              thanks for the help but i still don't understand why u can access a
              method in the same way u can an element of an array??

              Comment

              • Michael Winter

                #8
                Re: javascript array is not empty on creation

                On 6 Dec 2004 21:02:44 -0800, rusty <rusty@barrett. com.au> wrote:
                [color=blue]
                > ok, thanks for the link, it helped a lot!
                >
                > i found the spot where the clone method is added to the prototype of
                > Object, in a new library i recently included...
                >
                > thanks for the help but i still don't understand why u can access a
                > method in the same way u can an element of an array??[/color]

                object['property']

                is called square bracket notation. It's a basic language component that
                allows you to look up object properties using expressions. For example,
                say that an object has a set of properties called prop1, prop2, ..., prop9
                and you wanted to loop through all of them. Using square bracket notation,
                you would write:

                for(var i = 1; i <= 9; ++i) {
                object['prop' + i]
                }

                On each iteration, the numbers 1-9 would be concatenated with the string
                to produce the property name, which you could then query or modify.

                This method of property look-up also applies to arrays, too, but arrays
                treat numbers specially. If the string can be converted to a number, then
                back to a string, and still match the original value exactly, it is
                considered to be an array index, not a property. So:

                '10' -> 10 -> '10' '10' array index
                '05' -> 5 -> '5' '05' property name
                'fg' -> NaN -> 'NaN' 'fg' property name

                With objects, numbers are just property names, nothing more.

                See <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html> for
                a more detail description.

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Lee

                  #9
                  Re: javascript array is not empty on creation

                  rusty said:[color=blue]
                  >
                  >ok, thanks for the link, it helped a lot!
                  >
                  >i found the spot where the clone method is added to the prototype of
                  >Object, in a new library i recently included...
                  >
                  >thanks for the help but i still don't understand why u can access a
                  >method in the same way u can an element of an array??[/color]

                  This is not "chat". Please follow standard capitalization standards
                  and spell words completely.

                  Comment

                  • Grant Wagner

                    #10
                    Re: javascript array is not empty on creation

                    rusty wrote:
                    [color=blue]
                    > ok, thanks for the link, it helped a lot!
                    >
                    > i found the spot where the clone method is added to the prototype of
                    > Object, in a new library i recently included...
                    >
                    > thanks for the help but i still don't understand why u can access a
                    > method in the same way u can an element of an array??[/color]

                    You can:

                    <script type="text/javascript">
                    var o = {};
                    o.myMethod = function() { alert('hi'); }
                    o['myMethod']();

                    var myNewMethod = o['myMethod'];
                    myNewMethod();
                    </script>

                    --
                    Grant Wagner <gwagner@agrico reunited.com>
                    comp.lang.javas cript FAQ - http://jibbering.com/faq

                    Comment

                    • rusty

                      #11
                      Re: javascript array is not empty on creation

                      thanx for the help, i took a look at that page and now understand
                      square bracket notation

                      Comment

                      Working...