2 questions in JS

I

Isa

Hi every one !

1. I would like to know why \t and \n are not recognize. Is there a
version of Js that we must specify ? Also why document.write(...) and
writeln(...) give the same result ? Isn't supposed to have a newline
with writeln?

2. I would like to know more about Object oriented in JS. Here is my
code and I would like to know why it's not working? What am I doing
wrong ?

function Rectangle(l, h) { // constructor
this.largeur = l;
this.hauteur = h;
}

var rect1 = new Rectangle(2,4);
document.writeln("area of rect1 " + rect1.area( ) ); //<- area is not
called or accessed :-(

function area( ) {
return this.largeur * this.hauteur;
}

Thank you very much !
 
W

web.dev

Isa said:
Hi every one !

1. I would like to know why \t and \n are not recognize. Is there a
version of Js that we must specify ? Also why document.write(...) and
writeln(...) give the same result ? Isn't supposed to have a newline
with writeln?

The writeln method is the same as the write method, except the writeln
method appends a newline character to the end of the output. HTML
ignores the newline character, except within certain tags such as the
PRE tag. For example:

<pre>
<script type = "text/javascript">
document.writeln("hello");
document.writeln("world");
</script>
2. I would like to know more about Object oriented in JS. Here is my
code and I would like to know why it's not working? What am I doing
wrong ?

function Rectangle(l, h) { // constructor
this.largeur = l;
this.hauteur = h;
}

var rect1 = new Rectangle(2,4);
document.writeln("area of rect1 " + rect1.area( ) ); //<- area is not
called or accessed :-(

function area( ) {
return this.largeur * this.hauteur;
}

It is not working because your area() method is not a member of
Rectangle. Try the following instead:

function Rectangle(l, h)
{
this.largeur = l;
this.hauteur = h;
}

Rectangle.prototype.area = function()
{
return this.largeur * this.hauteur;
}

var rect1 = new Rectangle(2, 4);

alert("Area of rect1: " + rect1.area());
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 27 Sep 2006 10:04:44 remote, seen in
news:comp.lang.javascript said:
Isa wrote:

The writeln method is the same as the write method, except the writeln
method appends a newline character to the end of the output. HTML
ignores the newline character, except within certain tags such as the
PRE tag.

Newline is not ignored; it counts as whitespace, a separator, in
ordinary HTML.

It is well to generate a reasonable number of newlines in script-
generated HTML, if there is any chance that it will be read by a human,
a validator, etc.

It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top