473,583 Members | 4,428 Online
Bytes | Software Development & Data Engineering Community
+ 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 52268
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.prototyp e -, 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.protot ype.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('somet ype', 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
39787
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 date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear =...
1
375
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 to an enum. In the Media Catalog sample, I resorted to one giant switch statement that has a case block for each string that returns an enum from it....
3
10266
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps...
2
2845
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 bin2Date and there also is a Date2bin function of course in cannot find how to do this in csharp. The string looks like this "!$%\0" and should...
4
17674
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
2
2232
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
6776
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
369
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+)...
6
1972
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 type. Then I can do my check: If ( control is type) { //continue } I'm doing this inside of a recursive function which goes through the...
2
2207
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7825
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8179
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8323
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8191
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5372
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1155
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.