Arrays, string indices ... and more 
Author Message
 Arrays, string indices ... and more

Hello,
I have a few questions about Life, Universe and Everything:

------------------------------
Arrays:
when doing

var a = new Array();
a['one'] = 'one';
a['two'] = 'two';

var len = a.length;

len equals zero. Why?
------------------------------
Prototypes:

I added a little debug feature to Object.prototype:

Object.prototype.dbg = function()
{
  Response.write (<assorted info about the object>);

Quote:
}

But, when I do

for (var p in a)
  Response.write (p);

I also get the 'dbg' property, which I don't want. I only want to
iterate through the items, as the length property doesn't do what I
want(see above).

------------------------------
One last question:
how can I determine the name of an object from within the object?
I would like to print out the name of the object from my dbg() method.

Thoughts and ideas welcome.

Regards,

Jan Hvarfvenius



Sun, 09 Feb 2003 14:48:15 GMT  
 Arrays, string indices ... and more

Hi!

It's really interseting. As far as I understand when one begins using
associative arrays, JScript messes them up with objects. Here is an extract
from JScript 5.5 documentation:

"...In JScript, objects and arrays are almost identical to each other. The
two main differences are that normal objects do not have an automatic length
property, and arrays do not have the properties and methods of an
object...."

This sounds just fine, but according to your sample it happens to be that
you create an Array object, but then you use it as associative array it is
not an Array object anymore. Really strange... Looking forward to 'hear'
what M$ guys will say.

Jevgenij


Quote:
> Hello,
> I have a few questions about Life, Universe and Everything:

> ------------------------------
> Arrays:
> when doing

> var a = new Array();
> a['one'] = 'one';
> a['two'] = 'two';

> var len = a.length;

> len equals zero. Why?
> ------------------------------
> Prototypes:

> I added a little debug feature to Object.prototype:

> Object.prototype.dbg = function()
> {
>   Response.write (<assorted info about the object>);
> }

> But, when I do

> for (var p in a)
>   Response.write (p);

> I also get the 'dbg' property, which I don't want. I only want to
> iterate through the items, as the length property doesn't do what I
> want(see above).

> ------------------------------
> One last question:
> how can I determine the name of an object from within the object?
> I would like to print out the name of the object from my dbg() method.

> Thoughts and ideas welcome.

> Regards,

> Jan Hvarfvenius



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more

I believed that everything in JS was an object?
Or, rather, was derived from the Object object.

But it makes sense in a way:
when I add 'associative' items to the array, they are just stored as a
bunch of properties, and not as items in an array.

Is this a correct guess?

Jan Hvarfvenius (inquisitive)

On Wed, 23 Aug 2000 12:34:58 +0200, "Jevgenij Martynenko"

Quote:

>Hi!

>It's really interseting. As far as I understand when one begins using
>associative arrays, JScript messes them up with objects. Here is an extract
>from JScript 5.5 documentation:

>"...In JScript, objects and arrays are almost identical to each other. The
>two main differences are that normal objects do not have an automatic length
>property, and arrays do not have the properties and methods of an
>object...."

>This sounds just fine, but according to your sample it happens to be that
>you create an Array object, but then you use it as associative array it is
>not an Array object anymore. Really strange... Looking forward to 'hear'
>what M$ guys will say.

>Jevgenij



>> Hello,
>> I have a few questions about Life, Universe and Everything:

>> ------------------------------
>> Arrays:
>> when doing

>> var a = new Array();
>> a['one'] = 'one';
>> a['two'] = 'two';

>> var len = a.length;

>> len equals zero. Why?
>> ------------------------------
>> Prototypes:

>> I added a little debug feature to Object.prototype:

>> Object.prototype.dbg = function()
>> {
>>   Response.write (<assorted info about the object>);
>> }

>> But, when I do

>> for (var p in a)
>>   Response.write (p);

>> I also get the 'dbg' property, which I don't want. I only want to
>> iterate through the items, as the length property doesn't do what I
>> want(see above).

>> ------------------------------
>> One last question:
>> how can I determine the name of an object from within the object?
>> I would like to print out the name of the object from my dbg() method.

>> Thoughts and ideas welcome.

>> Regards,

>> Jan Hvarfvenius



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more

Arrays: 'one' and 'two' are not elements of the array a, but are properties of
the Array object a. The length property of an array is basically "one more
than the highest element index"; it has only a tenuous relationship to the
actual contents of the array. An example:

  var sparse=new Array();
  sparse[499]=0;
  var len=0;
  for (i in sparse) len++;
  WScript.echo(sparse.length + '\n' + len);

Prototypes: dbg isn't really a method of Object--it's a property that happens
to be a function object (talk about a subtle difference). If you don't want to
list method-like properties, try:

  for (var p in a) if (typeof(a[p]) != "function") Response.write (p);

One last answer: Don't know.

=-=-=
Steve
-=-=-

Quote:

> Hello,
> I have a few questions about Life, Universe and Everything:

> ------------------------------
> Arrays:
> when doing

> var a = new Array();
> a['one'] = 'one';
> a['two'] = 'two';

> var len = a.length;

> len equals zero. Why?
> ------------------------------
> Prototypes:

> I added a little debug feature to Object.prototype:

> Object.prototype.dbg = function()
> {
>   Response.write (<assorted info about the object>);
> }

> But, when I do

> for (var p in a)
>   Response.write (p);

> I also get the 'dbg' property, which I don't want. I only want to
> iterate through the items, as the length property doesn't do what I
> want(see above).

> ------------------------------
> One last question:
> how can I determine the name of an object from within the object?
> I would like to print out the name of the object from my dbg() method.

> Thoughts and ideas welcome.

> Regards,

> Jan Hvarfvenius



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more

Indeed interesting!

As for my second question, your answer took away an anxiety of mine:
having to exclude every 'method'(function object) by name. Your
solution does the trick, although there is a performance penalty, but
in release mode, you shouldn't need any debug output, should you?
So in release I search my code and comment out the 'if(typeof...)' and
the like.

You say 'one' and 'two' are not part of the array, but properties.
Does this mean that the associative array is not really an array, but
instead is a lot of properties? Well, it's obviously an array, but the
associative functionality comes not from the array but from the
Object, right?

Thanks for your input,

Jan Hvarfvenius(slightly relieved)

On Wed, 23 Aug 2000 08:31:39 -0400, "Steve Fulton"

Quote:

>Arrays: 'one' and 'two' are not elements of the array a, but are properties of
>the Array object a. The length property of an array is basically "one more
>than the highest element index"; it has only a tenuous relationship to the
>actual contents of the array. An example:

>  var sparse=new Array();
>  sparse[499]=0;
>  var len=0;
>  for (i in sparse) len++;
>  WScript.echo(sparse.length + '\n' + len);

>Prototypes: dbg isn't really a method of Object--it's a property that happens
>to be a function object (talk about a subtle difference). If you don't want to
>list method-like properties, try:

>  for (var p in a) if (typeof(a[p]) != "function") Response.write (p);

>One last answer: Don't know.

>=-=-=
>Steve
>-=-=-


>> Hello,
>> I have a few questions about Life, Universe and Everything:

>> ------------------------------
>> Arrays:
>> when doing

>> var a = new Array();
>> a['one'] = 'one';
>> a['two'] = 'two';

>> var len = a.length;

>> len equals zero. Why?
>> ------------------------------
>> Prototypes:

>> I added a little debug feature to Object.prototype:

>> Object.prototype.dbg = function()
>> {
>>   Response.write (<assorted info about the object>);
>> }

>> But, when I do

>> for (var p in a)
>>   Response.write (p);

>> I also get the 'dbg' property, which I don't want. I only want to
>> iterate through the items, as the length property doesn't do what I
>> want(see above).

>> ------------------------------
>> One last question:
>> how can I determine the name of an object from within the object?
>> I would like to print out the name of the object from my dbg() method.

>> Thoughts and ideas welcome.

>> Regards,

>> Jan Hvarfvenius



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more

Quote:
> It's really interseting. As far as I understand when one begins using
> associative arrays, JScript messes them up with objects. Here is an
extract
> from JScript 5.5 documentation:

Hi,

An Array in ECMAScript isn't like an array in other language (like, say,
C++). It's not a fixed-size chunk of memory. It is essentially just like a
normal ECMAScript Object that has a funky length property. All properties of
objects (including Array objects) are named with strings, but sometimes
those strings contian numbers. When you add a property to an array instance
with a name that looks like a number, JScript patches up the length property
(if necessary) to make sure it is always one more than the largest numbered
property. If your property name does not look like a number, it has no
effect on the length.

Quote:
> This sounds just fine, but according to your sample it happens to be that
> you create an Array object, but then you use it as associative array it is
> not an Array object anymore. Really strange... Looking forward to 'hear'
> what M$ guys will say.

It is still an array; as above, properties that don't look like numbers
don't affect the length.

Yes, this could be considered very weird behaviour, but it's just the way it
works ;-).

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more

Quote:
> len equals zero. Why?

Please see my other post

Quote:
> for (var p in a)
>   Response.write (p);

> I also get the 'dbg' property, which I don't want. I only want to
> iterate through the items, as the length property doesn't do what I
> want(see above).

That is because for...in also reads the prototype chain. You want to use:

    for (var p in a)
    {
        if (a.hasOwnProperty(p))
        {
            Response.Write(p);
        }
    }

Quote:
> ------------------------------
> One last question:
> how can I determine the name of an object from within the object?
> I would like to print out the name of the object from my dbg() method.

Errrrr, not really possible, unless you add a 'name' property to the object.
There is a possible hack involving enumerating the global object, but that
won't really work in the general case. So the simple answer is that there's
no way to do it.

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more
It's also worth mentioning that the length property is not the only
Array property that doesn't see non-numerical indices. All of the
array methods also ignore associative elements.





: > It's really interseting. As far as I understand when one begins
using
: > associative arrays, JScript messes them up with objects. Here is
an
: extract
: > from JScript 5.5 documentation:
:
: Hi,
:
: An Array in ECMAScript isn't like an array in other language (like,
say,
: C++). It's not a fixed-size chunk of memory. It is essentially just
like a
: normal ECMAScript Object that has a funky length property. All
properties of
: objects (including Array objects) are named with strings, but
sometimes
: those strings contian numbers. When you add a property to an array
instance
: with a name that looks like a number, JScript patches up the length
property
: (if necessary) to make sure it is always one more than the largest
numbered
: property. If your property name does not look like a number, it has
no
: effect on the length.
:
: > This sounds just fine, but according to your sample it happens to
be that
: > you create an Array object, but then you use it as associative
array it is
: > not an Array object anymore. Really strange... Looking forward to
'hear'
: > what M$ guys will say.
:
: It is still an array; as above, properties that don't look like
numbers
: don't affect the length.
:
: Yes, this could be considered very weird behaviour, but it's just
the way it
: works ;-).
:
: Peter
:
: --
: Peter J. Torr - Microsoft Windows Script Program Manager

: Please do not e-mail me with questions - post them to this
: newsgroup instead. Thankyou!
:
:



Sun, 09 Feb 2003 03:00:00 GMT  
 Arrays, string indices ... and more
Did we ever hear an answer to the (implied) question "can I find
out how many elements are in an 'associative array' without
counting them manually or writing my own count-them-manually
functino?"

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 13 Feb 2003 13:46:11 GMT  
 Arrays, string indices ... and more


Quote:
> Did we ever hear an answer to the (implied) question "can I find
> out how many elements are in an 'associative array' without
> counting them manually or writing my own count-them-manually
> functino?"

Hi,

There is no way to do it, other than as you suggest.

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Thu, 13 Feb 2003 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Array object with string indexes causes length property to always return 0

2. BinaryRead - howto convert binary string back to string array

3. Break Apart String Using Index

4. Break Apart String Using Index

5. ITERATE JSCRIPT ARRAYS WITH LABELS AS INDEXES

6. elements array index syntax

7. Index property in control array

8. Stuck -- need array index

9. How to sort/index array?

10. Array Allocation at high indexes..

11. Negative values in array index

12. Index in Array

 

 
Powered by phpBB® Forum Software