Treating a string as an array, and portability issues

B

Bart Lateur

A word of warning for you people who write their scripts and test them
on a single platform. Even in this day and age, when portability of
Javascript is a lot higher than it used to be, it might still bite you
in the arse.

For example. Try to run this in Firefox:

s = "ACE"; alert(s[1] );

You'll see a textbox showing a "C", the second character from the
string. So, Firefox treats a string, in traditional C manner, (also) as
an array of characters.

But! In MSIE (5.5, 6 and 7), this displays "undefined". So, MSIE does
not treat a string (also) as an array of characters.

n.b. I've tried to run this in Opera 9, and it displays a "C", too.

As a portable solution, use

s.substring(i, 1)

instead of

s


As a question to the experts: do you think s, with s a string,
*should* return the character in position i, from a theoretical point of
view?
 
R

RobG

Bart said:
A word of warning for you people who write their scripts and test them
on a single platform. Even in this day and age, when portability of
Javascript is a lot higher than it used to be, it might still bite you
in the arse.

For example. Try to run this in Firefox:

s = "ACE"; alert(s[1] );

You'll see a textbox showing a "C", the second character from the
string. So, Firefox treats a string, in traditional C manner, (also) as
an array of characters.

But! In MSIE (5.5, 6 and 7), this displays "undefined". So, MSIE does
not treat a string (also) as an array of characters.

The ECMAScript Language specification provides a "lowest common
denominator" of javascript support. Some - if not most - browsers have
extended the spec in places (as they are allowed to do) to provide
extra features and functionality. However, for the web you can't
specify a particular browser so you need to keep code compliant with
ECMA 262.

If you are on an intranet or have a special interest site and can
specify the supported browsers to be used, then you can program for
that environment.

[...]
As a question to the experts: do you think s, with s a string,
*should* return the character in position i, from a theoretical point of
view?

From a strictly theoretical viewpoint, my answer is no because a string
isn't an array and it doesn't have most of an Array's other properties
- in particular the extra methods (pop, slice, etc.). Giving it a
certain array-ness may create more problems than it solves (e.g. people
try to apply array methods to NodeLists and collections because they
have some array-like properties).

However, from a practical point of view, I think it's a good idea as
it's very convenient.

Then again, I'm no "expert". :)
 
B

Bart Lateur

RobG said:
From a strictly theoretical viewpoint, my answer is no because a string
isn't an array and it doesn't have most of an Array's other properties
- in particular the extra methods (pop, slice, etc.). Giving it a
certain array-ness may create more problems than it solves (e.g. people
try to apply array methods to NodeLists and collections because they
have some array-like properties).

Something other like this bit me earlier today.

I used the method getElementsByTagName like

a = document.forms[0].getElementsByTagName('input')

to get a list of input elements in a form, and then I tried to get only
those of type hidden, by chaining

a.filter(function (el) { return el.type=="hidden" })

to it (for Perlies: filter is Javascript's implementation of grep()).

However, this didn't work, because the "array" result a didn't have a
method "filter".

The result of getElementsByTagName may look like an array, behave like
an array for most parts, such as indexing by integer and having a
length; but it *is not* an array.

After copying each element to a real array, filter did work on the real
array.
 
R

RobG

Bart said:
RobG said:
From a strictly theoretical viewpoint, my answer is no because a string
isn't an array and it doesn't have most of an Array's other properties
- in particular the extra methods (pop, slice, etc.). Giving it a
certain array-ness may create more problems than it solves (e.g. people
try to apply array methods to NodeLists and collections because they
have some array-like properties).

Something other like this bit me earlier today.

I used the method getElementsByTagName like

a = document.forms[0].getElementsByTagName('input')

to get a list of input elements in a form, and then I tried to get only
those of type hidden, by chaining

a.filter(function (el) { return el.type=="hidden" })

to it (for Perlies: filter is Javascript's implementation of grep()).

However, this didn't work, because the "array" result a didn't have a
method "filter".

The result of getElementsByTagName may look like an array, behave like
an array for most parts, such as indexing by integer and having a
length; but it *is not* an array.

There are some difficulties if you try to implement an HTMLCollection
as an array given that collections are 'live'. If you pop() an
element, that should remove it from the document, not just the
collection. While collections are a bit like arrays, I can understand
why they are kept quite separate.
After copying each element to a real array, filter did work on the real
array.

Expect to be bitten again: Array.prototype.filter is a function in
Firefox but is undefined for Opera and IE (and probably most other
browsers).
 
D

Dr J R Stockton

In comp.lang.javascript message <bladq25pssmvjts7uc44lda8dvks2futpr@4ax.
As a question to the experts: do you think s, with s a string,
*should* return the character in position i, from a theoretical point of
view?


If so, should one not expect s = "#" to set that character to "#" ?
It would be a reasonable hope. But then what should s = "fred" do?

In IE6,
S = new String("abc")
S[1] = 999
alert(S[1])
alerts 999 - what should one expect with [] indexing available?

ISTM that s is an excellent notation, incompatible with javascript;
but it could be introduced, read/write, using a different form of
bracket. Writing complicates the semantics of String, though.

If s = "#" does NOT set character i to "#", then IMHO x = s
should NOT set x to the ith character.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top