By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,718 Members | 1,220 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,718 IT Pros & Developers. It's quick & easy.

String object question

P: n/a

<html>
<head>
<script>
var s=String( 'foo' );
alert( s );
s.bar='bar';
alert( s.bar );
</script></head></html>

Why does the second alert produce 'undefined'? Are string objects
somehow special?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Christopher Benson-Manica wrote:

<html>
<head>
<script>
var s=String( 'foo' );
alert( s );
s.bar='bar';
alert( s.bar );
</script></head></html>

Why does the second alert produce 'undefined'? Are string objects
somehow special?


Shouldn't it be something like this?

<html>
<head>
<script>
var s=new String();
s.first = "First string";
alert(s.first);
s.second = "Second string";
alert(s.second);
</script>
</head>
</html>
Jul 23 '05 #2

P: n/a
PDannyD <da*****@scenicplace.freeserve.co.uk> spoke thus:
Shouldn't it be something like this? var s=new String();


Yes, you're right. My way was trying to assign properties to a
primitive string and of course you can't do that. Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #3

P: n/a
Lee
Christopher Benson-Manica said:


<html>
<head>
<script>
var s=String( 'foo' );
alert( s );
s.bar='bar';
alert( s.bar );
</script></head></html>

Why does the second alert produce 'undefined'? Are string objects
somehow special?


You didn't create a String object, just a simple string.
Try it with:

var s = new String( 'foo' );

Jul 23 '05 #4

P: n/a
Lee <RE**************@cox.net> spoke thus:
You didn't create a String object, just a simple string.
Try it with: var s = new String( 'foo' );


Yes, that works. Here's a followup question though: Does the split()
method produce an array of string objects or primitive strings?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #5

P: n/a
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> spoke thus:
You didn't create a String object, just a simple string.
Try it with:

var s = new String( 'foo' );


Yes, that works. Here's a followup question though: Does the split()
method produce an array of string objects or primitive strings?


They are primitive strings.
The following alerts "string", rather than "object":

<html>
<body>
<script type="text/javascript">
var token="alpha,beta,gamma".split(",");
alert(typeof token[0]);
</script>
</body>
</html>

Jul 23 '05 #6

P: n/a
Christopher Benson-Manica wrote:
Lee <RE**************@cox.net> spoke thus:
You didn't create a String object, just a simple string.
Try it with:
var s = new String( 'foo' );


Yes, that works. Here's a followup question though: Does the split()
method produce an array of string objects or primitive strings?

--
Christopher Benson-Manica | I *should* know what I'm talking about -

if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


This may help:

http://www.chinalinuxpub.com/doc/ore...pt/ch03_11.htm

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.