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[i]
As a question to the experts: do you think s[i], with s a string,
*should* return the character in position i, from a theoretical point of
view?
--
Bart. 6 1636
Bart Lateur wrote:
As a portable solution, use
s.substring(i, 1)
I prefer to do the following instead:
str.charAt(i);
Bart Lateur wrote:
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[i], 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". :-)
--
Rob
RobG wrote:
>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.
--
Bart.
Bart Lateur wrote:
RobG wrote:
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).
--
Rob
In comp.lang.javascript message <bladq25pssmvjts7uc44lda8dvks2futpr@4ax.
com>, Thu, 11 Jan 2007 21:29:40, Bart Lateur <ba*********@pandora.be>
posted:
>As a question to the experts: do you think s[i], with s a string, *should* return the character in position i, from a theoretical point of view?
If so, should one not expect s[i] = "#" to set that character to "#" ?
It would be a reasonable hope. But then what should s[i] = "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[i] 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[i] = "#" does NOT set character i to "#", then IMHO x = s[i]
should NOT set x to the ith character.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Bart Lateur wrote:
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] );
s = "ACE".split(""); alert(s[1]);
Mick. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fabio de francesco |
last post by:
Hi,
I'm not a professional programmer, but I've been writing C/C++ and Ada
programs for a few years on GNU/Linux without ever concerning on
standards and portability to other OSs.
I've always...
|
by: Khuong Dinh Pham |
last post by:
I have the contents of an image of type std::string. How can I make a
CxImage object with this type.
The parameters to CxImage is:
CxImage(byte* data, DWORD size)
Thx in advance
|
by: asm |
last post by:
Hi All,
Like typdef, does C have further support for portability?
Thanks,
ASM
|
by: roman ziak |
last post by:
I just read couple articles on this group and it keeps amazing me how
the portability is used as strong argument for language cleanliness.
In my opinion, porting the program (so you just take the...
|
by: n00dle |
last post by:
hi,
see if i want to copy the contents of a char arrary into a equally
size struct, assuming that the structure has no holes, padding etc., i
can use
structure = *(t_structure *) array;
but...
|
by: Nascimento |
last post by:
Hello,
How to I do to return a string as a result of a function.
I wrote the following function:
char prt_tralha(int num)
{
int i;
char tralha;
|
by: John Fly |
last post by:
I've used standard functions like find_first_of and find_first_not_of
for some time. I was about to implement a quick string tokenizer
similar to the example below when I ran across an old thread...
|
by: Brand Bogard |
last post by:
Does the C standard include a library function to convert an 8 bit character
string to a 16 bit character string?
|
by: jacob navia |
last post by:
One of the holy cows here is this "portability" stuff.
In practice, portability means:
1) Use the least common denominator of all the supported
systems.
2) Between usability / good user...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |