473,387 Members | 1,561 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,387 software developers and data experts.

How to print a rectangle construct variabe in JavaScript?

I am doing examples in "javascript: the Definitive Guide." For Example

8-1, a rectangle constructor function, how do I print/write the
rectagle (x,y)?
Here is the JavaScript program:
function Rectangle(w, h)
{
this.width = w;
this.height = h;

}
var rect1 = new Rectangle(2, 4);
var rect2 = new Rectangle(8.5, 11);

I tried to print "Rectangle (2,4)", by coding:
document.write("<h2>Rectangle</h2>");
document.write(rect1);
Previewing on FrontPage showed: Rectangle [object Object]
TIA,
Jeffrey

Nov 5 '05 #1
3 2235
<cj*******@gmail.com> skrev i meddelandet
news:11*********************@g47g2000cwa.googlegro ups.com...
I am doing examples in "javascript: the Definitive Guide." For Example

8-1, a rectangle constructor function, how do I print/write the
rectagle (x,y)?
Here is the JavaScript program:
function Rectangle(w, h)
{
this.width = w;
this.height = h;

}
var rect1 = new Rectangle(2, 4);
var rect2 = new Rectangle(8.5, 11);

I tried to print "Rectangle (2,4)", by coding:
document.write("<h2>Rectangle</h2>");
document.write(rect1);


If you want do print "Rectangle(2,4)", then that's what you'll have to give
to document.write.
Objects aren't turned to strings by magic.

You can access the rectangle properties, and could write a function such as:

function writeRectangle(inRect){
document.write("Rectangle(" + inRect.width + ", " + inRect.height +
")");
}

--
Joakim Braun
Nov 5 '05 #2
wrote:
document.write("<h2>Rectangle</h2>");
document.write(rect1);
Previewing on FrontPage showed: Rectangle [object Object]


If you want a sensible representation of an object you have to tell the
system how to convert it to a string.

Try adding this after your definition of the Rectangle function:

Rectangle.prototype.toString = function() {
return "Rectangle("+this.width+","+this.height+")";
}
Nov 5 '05 #3
Thanks.

Nov 6 '05 #4

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

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
10
by: Tyrant Mikey | last post by:
I don't know if this can even be done, but I'd REALLY like to eliminate the selection rectangle that appears around my ImageButton controls when the user clicks on them. The buttons are on a black...
1
by: Praveen | last post by:
Hello, I have a web page which will display the map of a city/place. I have a toolbar which contains 'zoom in' and 'zoom out' buttons. When the user clicks on zoom in/zoom out button, he should...
9
by: Kevin | last post by:
I would like to print the panel control. The panel controls contains images and lables. If i can capture as image i can use it for other purposes like Zooming etcc.. Any body have any idea...
0
by: B-Dog | last post by:
I'm using this class here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/printwinforms.asp to build a tabular print report and it works fine but when I try and add...
0
by: clashway | last post by:
It took me so long to figure this out, I thought I would share it. This prints an MSChart using the metafilepict format after copying the chart to the clipboard. This provides a high-resolution...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: Mo | last post by:
Hi, I have two tiff images which present the front and back of a card. I need to send these to images to a printer that has a two sided printing capabilities. I need to send these two images to...
1
by: gleave | last post by:
Hi, below is the code i got to print a datagridview, the trouble is it also prints visible columns which i don't want to be printed. Does anyone have any ideas on how i could change the code so it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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,...

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.