473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

objects with string indices

I have a stumper, not sure if it belongs here or in
mozilla.dev.tech.js-engine:

<script language=javascript>
x='123'+'\0'+'456';
y='123'+'\0'+'789';
a={};
a[x]=1;
a[y]=2;
s='(x==y): '+(x==y)+'\na[x]: '+a[x]+'\na[y]: '+a[y]+'\na["123"]:
'+a["123"];
alert(s);
</script>

On Mozilla (and in JSDB which also uses Spidermonkey) this prints
(x==y): false
a[x]: 2
a[y]: 2
a["123"]: 2

It appears object indices use a different equality metric than
Javascript's string equality operator "==", namely that Javascript
strings have an overall length and don't just stop at an embedded null
character, whereas object indices seem to stop at an embedded null.

Is this behavior documented somewhere (and is it intentional or a bug)?

Nov 16 '06 #1
7 1268
In article <11**********************@m7g2000cwm.googlegroups. com>, Jason
S <jm*****@gmail.comwrites

<snip>
>It appears object indices use a different equality metric than
Javascript's string equality operator "==", namely that Javascript
strings have an overall length and don't just stop at an embedded null
character, whereas object indices seem to stop at an embedded null.

Is this behavior documented somewhere (and is it intentional or a bug)?
According to ECMA 262 you can have any characters you like in a property
name. It looks as though you've found a bug, either in a browser
implementation or in ECMA 262 (i.e they forgot to ban \0 in property
names).

I expect the unofficial answer is "why would anyone want to do that".

John
--
John Harris
Nov 16 '06 #2
John G Harris wrote:
I expect the unofficial answer is "why would anyone want to do that".
I was trying to figure out a way to do 2-D arrays, using the \0 as a
separator... though I suppose if you are doing some kind of hash lookup
based on binary data, this would be a valid method as well...

Nov 16 '06 #3
VK

Jason S wrote:
I have a stumper, not sure if it belongs here or in
mozilla.dev.tech.js-engine:

<script language=javascript>
x='123'+'\0'+'456';
y='123'+'\0'+'789';
a={};
a[x]=1;
a[y]=2;
s='(x==y): '+(x==y)+'\na[x]: '+a[x]+'\na[y]: '+a[y]+'\na["123"]:
'+a["123"];
alert(s);
</script>

On Mozilla (and in JSDB which also uses Spidermonkey) this prints
(x==y): false
a[x]: 2
a[y]: 2
a["123"]: 2
JavaScript is fully Unicode-driven, this way it is a question why would
any compliant engine make some special relationship studies between
\u0000 (NUL) and \u0030 (zero sign).

That is also a question why the silly \0 delimiter from Cx languages
would keep its special value in JavaScript - but it comes close to an
evangelism discussion :-)

Nov 16 '06 #4
In article <11*********************@h54g2000cwb.googlegroups. com>, VK
<sc**********@yahoo.comwrites

<snip>
>That is also a question why the silly \0 delimiter from Cx languages
would keep its special value in JavaScript - but it comes close to an
evangelism discussion :-)
It didn't "keep its special value". \0 is not the null character in
ECMAScript version 2, but it is in version 3.

It looks as though \0 was added in version 3 because so many people like
VK wanted it.

John
--
John Harris
Nov 17 '06 #5
VK

John G Harris wrote:
It didn't "keep its special value". \0 is not the null character in
ECMAScript version 2, but it is in version 3.
15.10.2.11 DecimalEscape is the only place vaguely mentioning \0 and
NUL but right - I was deeply wrong.
It looks as though \0 was added in version 3 because so many people like
VK wanted it.
I wanted NUL for a language where strings are not null-terminated? No,
no, no! :-)
I played a bit with that freshly discovered (for me) NUL. While string
methods acting as they possibly(?) should if strings are not
null-terminated, overall the engine seems pretty much FOBAR (in the US
Army sense of this acronym):

<script language=javascript>
x='123'+'\0'+'456';
alert(x); // 123
alert(x.length); // 7
alert(x.charAt(6)); // 6
a={};
a[x] = 1;
for (var p in a) {
alert(''+p); // 123
alert((''+p).length); // 7
alert((''+p).charAt(6)); // 6
}

alert(x == '123');
</script>

Nov 18 '06 #6
Apparently it was a bug. If you change '123' to 'abc' it works as
expected; something to do with the index parser and the dual nature of
indices (numbers vs. strings).

I have another one that I seem to have stumbled on:
<script language=javascript>
document.open();
s = 'abcdefg';
over=2;
for (i = -over; i < s.length+over; i++)
{
document.writeln('s['+i+']='+s[i]+'<br>');
}
document.close();
</script>

I get:
s[-2]=undefined
s[-1]=7
s[0]=a
s[1]=b
s[2]=c
s[3]=d
s[4]=e
s[5]=f
s[6]=g
s[7]=undefined
s[8]=undefined

what's special about index -1 that it returns length, apparently?

Nov 21 '06 #7
Jason S wrote:
Apparently it was a bug.
Yes, that was a bug.

<snip.I have another one that I seem to have stumbled on:
<script language=javascript>
document.open();
s = 'abcdefg';
over=2;
for (i = -over; i < s.length+over; i++)
{
document.writeln('s['+i+']='+s[i]+'<br>');
}
document.close();
</script>

I get:
s[-2]=undefined
s[-1]=7
s[0]=a
s[1]=b
s[2]=c
s[3]=d
s[4]=e
s[5]=f
s[6]=g
s[7]=undefined
s[8]=undefined
That is not a bug, it is an extension. There are no integer property
names defined for String object so finding an example where they exist,
and finding that a property with the name "-1" is the length of the
string, is just a feature of that specific implementation. Neither
significant nor useful (outside things like Firefox/Gecko extensions).

Richard.
what's special about index -1 that it returns length, apparently?
Nov 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Richard A. DeVenezia | last post by:
Can someone explain why JavaScript arrays are not allowed to have objects as indices ? Would solve many a hashtable lookup problems.... -- Richard A. DeVenezia
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: ankit | last post by:
I want to get the start and end of all the patterns mattched in regex. I know I can get it with start() and end() fn of matched objects. But re.search() return the match object of first matching...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.