Connecting Tech Pros Worldwide Forums | Help | Site Map

javascript array is not empty on creation

rusty
Guest
 
Posts: n/a
#1: Jul 23 '05
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
Guest
 
Posts: n/a
#2: Jul 23 '05

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

Lee
Guest
 
Posts: n/a
#3: Jul 23 '05

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.

rusty
Guest
 
Posts: n/a
#4: Jul 23 '05

re: javascript array is not empty on creation


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

RobG
Guest
 
Posts: n/a
#5: Jul 23 '05

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

Have a look here:

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


--
Rob
rusty
Guest
 
Posts: n/a
#6: Jul 23 '05

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

rusty
Guest
 
Posts: n/a
#7: Jul 23 '05

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

Michael Winter
Guest
 
Posts: n/a
#8: Jul 23 '05

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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Lee
Guest
 
Posts: n/a
#9: Jul 23 '05

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.

Grant Wagner
Guest
 
Posts: n/a
#10: Jul 23 '05

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@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

rusty
Guest
 
Posts: n/a
#11: Jul 23 '05

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

Closed Thread