javascript array is not empty on creation

Discussion in 'Javascript' started by rusty, Dec 7, 2004.

  1. rusty

    rusty Guest

    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;
    try { if (value != null && typeof (value) == "object" && value !=
    window && !value.nodeType) { value.clone = Object.clone; copy =
    value.clone(); } else { copy = value; } } catch (e) { copy =
    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, Dec 7, 2004
    #1
    1. Advertising

  2. rusty

    rusty Guest

    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...
     
    rusty, Dec 7, 2004
    #2
    1. Advertising

  3. rusty

    Lee Guest

    rusty said:
    >
    >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...


    It's not an element of the array, it's a method of the Array object.
     
    Lee, Dec 7, 2004
    #3
  4. rusty

    rusty Guest

    if its a method, why can u access it using admin_types['clone'] ??
     
    rusty, Dec 7, 2004
    #4
  5. rusty

    RobG Guest

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


    admin_types['clone']

    is the same as:

    admin_types.clone

    Have a look here:

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


    --
    Rob
     
    RobG, Dec 7, 2004
    #5
  6. rusty

    rusty Guest

    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??
     
    rusty, Dec 7, 2004
    #6
  7. rusty

    rusty Guest

    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??
     
    rusty, Dec 7, 2004
    #7
  8. On 6 Dec 2004 21:02:44 -0800, rusty <> wrote:

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


    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.com/faq/faq_notes/square_brackets.html> for
    a more detail description.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Dec 7, 2004
    #8
  9. rusty

    Lee Guest

    rusty said:
    >
    >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??


    This is not "chat". Please follow standard capitalization standards
    and spell words completely.
     
    Lee, Dec 7, 2004
    #9
  10. rusty

    Grant Wagner Guest

    rusty wrote:

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


    You can:

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

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

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Dec 7, 2004
    #10
  11. rusty

    rusty Guest

    thanx for the help, i took a look at that page and now understand
    square bracket notation
     
    rusty, Dec 7, 2004
    #11
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,039
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    820
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    582
    Dave Thompson
    Feb 14, 2004
  4. ButlerDJIAM
    Replies:
    0
    Views:
    569
    ButlerDJIAM
    Nov 9, 2006
  5. Zhukov Pavel

    Big empty file creation

    Zhukov Pavel, Jun 28, 2008, in forum: Ruby
    Replies:
    6
    Views:
    133
    Robert Klemme
    Jun 28, 2008
Loading...

Share This Page