473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert object to string

Max
I just wonder if there is a way of converting an object to a string.
I am using a function to send an object parameter e.g "fctng(train0);"
later on I would like to use train0 but as a string to access a
img id called <img id="train0"> .
This might be obvious to you but I am not a programmer, just trying to
learn javascript at home for fun.
Thanks

Max
Jul 23 '05 #1
2 52252
Hi Max,
I just wonder if there is a way of converting an object to a string.


Yes and no. It depends on the situation. Are you wanting to convert a
javascript object or a user-defined object? All javascript objects
have a toString method, however if it's an object that you've made
yourself, you'll need to provide a way to convert it to a string.

Jul 23 '05 #2
Max wrote:
I just wonder if there is a way of converting an object
to a string.
In loosely typed javascript any type can be converted into any other
type, and they are automatically internally converted to resolve
expressions whenever the context of use requires a different type. See:-

<URL: http://www.jibbering.com/faq/faq_not...e_convert.html >
The rule for converting Objects to strings is that the object's -
toString - method is called and the result is the string primitive value
of the object. Objects inherit default - toString - methods from their
prototype. For objects created with the object constructor, constructor
functions and object literals that - toString - function is the one
defined on the - Object.prototype -, which is not particularly useful as
it is specified as returning "[object Object]".

You get around the default - toString - methods of objects by providing
your own. EG:-

function MyObject(type, value){
this.type = type;
this.value = value;
}
MyObject.prototype.toString = function(){
return (
"[MyObject: \n\ttype = "+
this.type+
"\n\tvalue = "+
this.value+
"\n]\n"
);
};

- giving an object that when type-converted to a string (or as a result
of having its toString method called directly) will return a string
describing the object's current state. I.E.:-

var obj = new MyObject('sometype', 55);

var str = ''+obj; //concatenation forces obj's type-conversion to a
string.

alert(str); // --> outputs:-

[MyObject:
type = sometype
value = 55
]

- in the alert box.

The same result will follow from calling the object's - toString -
method directly as:-

obj.toString();
I am using a function to send an object parameter e.g
"fctng(train0);" later on I would like to use train0 but
as a string to access a img id called <img id="train0"> .
That is not a sufficient explanation of what you are attempting (or why)
to convey meaning. Remember, code always contributes to understanding
when you are talking about scripting.
This might be obvious to you but I am not a programmer,
just trying to learn javascript at home for fun.


No harm in that. Look at the information that the FAQ provides on the
subject of asking questions, it will help in the long run.

Richard.
Jul 23 '05 #3

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

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
2
by: Remi Caron | last post by:
Hi, I took over an Visual Object project (Visual Clipper) in that language there is a function to: Convert a string containing a 32-bit binary date to a date data type. That function is called...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
2
by: Ronchese | last post by:
Hi all. Is possible I coerce a value to a determined type informed by a string? See the sample: dim obj as Object obj = SomeConversionType(2, "System.Windows.Forms.DockStyle")
5
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
6
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
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...
0
Oralloy
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.