| re: How to print a rectangle construct variabe in JavaScript?
<cjeffwang@gmail.com> skrev i meddelandet
news:1131170736.021547.23160@g47g2000cwa.googlegro ups.com...[color=blue]
> 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);[/color]
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 |