2 questions in JS

Discussion in 'Javascript' started by Isa, Sep 27, 2006.

  1. Isa

    Isa Guest

    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 !
    Isa, Sep 27, 2006
    #1
    1. Advertising

  2. Isa

    web.dev Guest

    Isa wrote:
    > 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>
    </pre>

    > 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());
    web.dev, Sep 27, 2006
    #2
    1. Advertising

  3. JRS: In article <>,
    dated Wed, 27 Sep 2006 10:04:44 remote, seen in
    news:comp.lang.javascript, web.dev <> posted :
    >
    >Isa wrote:


    >> 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.


    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.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Sep 27, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    551
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,112
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    545
  4. Vijay
    Replies:
    2
    Views:
    582
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    261
    reema
    Aug 26, 2008
Loading...

Share This Page