javascript array is not empty on creation

R

rusty

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
 
R

rusty

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

Lee

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

rusty

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

rusty

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

Michael Winter

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

Lee

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

Grant Wagner

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

You can:

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

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

rusty

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top