Strings as arrays

T

Tim Streater

Safari and FF seem to allow this:

var wiggy = "ABCD";

ch = wiggy[2]; // ch will contain the character 'C'

however my JS book seems to insist that I do this:

ch = wiggy.charAt(2);

and indeed doesn't appear to mention the first method at all.

Since for my particular purpose I want to treat the string as an array
of single characters, I prefer the first method rather than the second.
Is there any reason not to pursue this approach?

Thanks,

-- tim
 
T

Tom Cole

Safari and FF seem to allow this:

var wiggy = "ABCD";

ch = wiggy[2]; // ch will contain the character 'C'

however my JS book seems to insist that I do this:

ch = wiggy.charAt(2);

and indeed doesn't appear to mention the first method at all.

Since for my particular purpose I want to treat the string as an array
of single characters, I prefer the first method rather than the second.
Is there any reason not to pursue this approach?

Thanks,

-- tim

Only if you wish to make your application inaccessible to IE users. It
returns [undefined] to them.
 
E

Elegie

Tim Streater wrote:

Hi,
Since for my particular purpose I want to treat the string as an array
of single characters, I prefer the first method rather than the second.
Is there any reason not to pursue this approach?

An excellent one: it is not part of ECMAScript 262, and as such, is not
something browser vendors have to implement.

If you don't want to use the 'chatAt' method, one alternative could be
to transform your string into an array, using the 'split' method.


Kind regards,
Elegie.
 
T

Tim Streater

Elegie said:
Tim Streater wrote:

Hi,


An excellent one: it is not part of ECMAScript 262, and as such, is not
something browser vendors have to implement.

Hmm, yes, I rather thought that might be the case, but thanks for
confirming it.
If you don't want to use the 'charAt' method, one alternative could be
to transform your string into an array, using the 'split' method.

I'll probably just stick with charAt. I haven't started implementing
this part of my pages yet, but I want to have something I can treat as
an array while manipulating it, but pass as a single entity between
pages using something like:

document.forms['xxx'].timeslots.value = wiggy;


where I also have:

<form name='xxx'>
<input type=hidden name=timeslots>
</form>


(timeslots will go into a mysql table as char(64)).

-- tim
 
T

Tim Streater

Tom Cole said:
Safari and FF seem to allow this:

var wiggy = "ABCD";

ch = wiggy[2]; // ch will contain the character 'C'

however my JS book seems to insist that I do this:

ch = wiggy.charAt(2);

and indeed doesn't appear to mention the first method at all.

Since for my particular purpose I want to treat the string as an array
of single characters, I prefer the first method rather than the second.
Is there any reason not to pursue this approach?

Thanks,

-- tim

Only if you wish to make your application inaccessible to IE users. It
returns [undefined] to them.

Round here, I'd probably get applause for that :)

-- tim
 
T

Tim Streater

Michael White said:
Tim said:
Tim Streater wrote:

Hi,


Since for my particular purpose I want to treat the string as an array
of single characters, I prefer the first method rather than the second.
Is there any reason not to pursue this approach?

An excellent one: it is not part of ECMAScript 262, and as such, is not
something browser vendors have to implement.


Hmm, yes, I rather thought that might be the case, but thanks for
confirming it.

If you don't want to use the 'charAt' method, one alternative could be
to transform your string into an array, using the 'split' method.


I'll probably just stick with charAt. I haven't started implementing
this part of my pages yet, but I want to have something I can treat as
an array while manipulating it, but pass as a single entity between
pages using something like:

document.forms['xxx'].timeslots.value = wiggy;

document.forms['xxx'].timeslots.value = "abc".split("");

Mick

Mick,

Could you expand on this a little? Is there something actually wrong (in
the sense e.g. that it is non-standard, or some other issue) with it?
What I need is for the hidden variable timeslots in the form below to
get the value of the string wiggy.

Thanks -- tim

-- tim
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top