Why wont this script work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ali

    #1

    Why wont this script work?

    I have the following web page with a script in it.

    <html>

    <head>

    <title>Make Your Own Objects test</title>

    <script>

    function info(self,n,e) {

    function showInfo(self,n am, ema) {

    document.write( "<hr>");

    document.write( "<b>Name: </b>"+nam);

    document.write( "<b>Email: </b>"+ema);

    }

    self.name = n;

    self.email= e;
    self.show() = showInfo(self.n ame,self.email) ;

    }

    </script>

    </head>

    <body>

    <script>

    ali = info("Ali","ali @ali.com");

    zainab = info("Zainab"," zainab@zainab.c om");


    ali.show();
    zainab.show();

    </script>

    </body>

    </html>

    I want the script to make two objects: ali and zainab. These objects
    are to have two properties (name and email) and a method (show). The
    show method is supposed to display the other two properties of the
    object.

    Please Help. Thank You. :)
  • Lee

    #2
    Re: Why wont this script work?

    Ali said:[color=blue]
    >
    >I have the following web page with a script in it.[/color]
    [color=blue]
    >I want the script to make two objects: ali and zainab. These objects
    >are to have two properties (name and email) and a method (show). The
    >show method is supposed to display the other two properties of the
    >object.[/color]

    Is this for a class, or are you trying to learn Javascript on your own?
    Are you working from some sort of textbook or online tutorial?

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Why wont this script work?

      Ali wrote:
      [color=blue]
      > <html>[/color]

      The DOCTYPE declaration is missing prior to this start tag, e.g.

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      [color=blue]
      > [...]
      > <script>[/color]
      ^
      The "type" attribute is missing:

      <script type="text/javascript">
      [color=blue]
      > function info(self,n,e) {
      >
      > function showInfo(self,n am, ema) {[/color]

      Nested functions are not allowed in ECMAScript and implementations .
      However, it is supported in JavaScript 1.5. The question probably
      answered below is: Why do you consider it necessary here?
      [color=blue]
      > document.write( "<hr>");
      >
      > document.write( "<b>Name: </b>"+nam);
      >
      > document.write( "<b>Email: </b>"+ema);
      >
      > }
      >
      > self.name = n;[/color]

      `self' refers to the current `window' object as this object is virtually
      identical with the global object where the `self' property is available.
      Assigning a value to its `name' property may not be possible. Most
      certainly you are looking for `this.name'.
      [color=blue]
      > self.email= e;
      > self.show() = showInfo(self.n ame,self.email) ;[/color]
      ^^
      The call operator may only be used right hand side in JavaScipt,
      ECMAScript and its implementations .
      [color=blue]
      > }
      >
      > [...}
      > <script>[/color]

      See above.
      [color=blue]
      > ali = info("Ali","ali @ali.com");
      > zainab = info("Zainab"," zainab@zainab.c om");[/color]

      Variables should be declared, using the `var' keyword:

      var ali = ...;
      var zainab = ...;

      Assuming that "info" is the constructor of an object, the proper syntax is

      var ali = new info("Ali", "ali@ali.co m");
      var ali = new info("Zainab"," zainab@zainab.c om");
      [color=blue]
      > ali.show();
      > zainab.show();>[/color]

      However, if show() ought to be a method of "info" objects, the proper syntax
      is

      function info(n, e)
      {
      this.show = function info_show()
      {
      // escape ETAGO if used in HTML context
      document.write( "<hr>"
      + "<b>Name: <\/b>" + this.name
      + "<b>Email: <\/b>" + this.email);
      }

      // consider ... = x || ""; for a default value
      this.name = n;
      this.email = e;
      }

      or better if info_show() does not contain a closure:

      function info(n, e)
      {
      // see above
      this.name = n;
      this.email = e;
      }

      info.prototype. show = function info_show()
      {
      document.write( "<hr>"
      + "<b>Name: <\/b>" + this.name
      + "<b>Email: <\/b>" + this.email);
      }

      However, document.write( ) is not a proper method to output information
      after the document has been loaded. It may add information to the
      document temporarily or it may overwrite the entire document, including
      the functions defined therein. Ask Google Groups for proper solutions.

      To avoid confusion and undesired side effects with built-in properties,
      good code style recommends to let constructor identifiers begin with an
      uppercase letter:

      function Info(...)
      {
      ...
      }
      [color=blue]
      > [...]
      > I want the script to make two objects: ali and zainab. These objects
      > are to have two properties (name and email) and a method (show). The
      > show method is supposed to display the other two properties of the
      > object.[/color]

      And most certainly, you should have added that the properties
      of the second object are not displayed (as expected).

      "Does not work" is a useless error description. [psf 4.11]

      Please read the FAQ before posting.


      PointedEars
      --
      As the "BugFree(tm )" series didn't turn out so well, i'm starting a new
      series called "ItWorksForMe(t m)" of which this new (browser) is yet another
      shining example...

      Comment

      • John G Harris

        #4
        Re: Why wont this script work?

        In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
        Lahn <PointedEars@we b.de> writes

        <snip>[color=blue]
        >Nested functions are not allowed in ECMAScript and implementations .
        >However, it is supported in JavaScript 1.5.[/color]
        <snip>

        It is allowed in ECMAScript v3, which has been around for over four
        years.

        John
        --
        John Harris

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Why wont this script work?

          John G Harris wrote:
          [color=blue]
          > In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
          > Lahn <PointedEars@we b.de> writes[/color]

          Please stop posting attribution novels; the reasons have been explained
          (to you) several times before.
          [color=blue][color=green]
          >> Nested functions are not allowed in ECMAScript and implementations .
          >> However, it is supported in JavaScript 1.5.[/color]
          >
          > It is allowed in ECMAScript v3, which has been around for over four
          > years.[/color]

          I'd be happy to be wrong here and read about the required productions
          from the ECMAScript 3 grammar in a posting of you.


          PointedEars
          --
          Dance, Mozilla, Dance!

          Comment

          • Randy Webb

            #6
            Re: Why wont this script work?

            Thomas 'PointedEars' Lahn wrote:
            [color=blue]
            > John G Harris wrote:
            >
            >[color=green]
            >>In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
            >>Lahn <PointedEars@we b.de> writes[/color]
            >
            >
            > Please stop posting attribution novels; the reasons have been explained
            > (to you) several times before.[/color]

            Considering that you are the only one that explains it, and you
            generally and 100% explain it wrongly, thats just more of your unfounded
            garbage spouting.

            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq

            Comment

            • Michael Winter

              #7
              ECMAScript FunctionDeclara tion Grammar (was: Re: Why wont this script work?)

              On Sun, 17 Oct 2004 23:26:27 +0200, Thomas 'PointedEars' Lahn
              <PointedEars@we b.de> wrote:
              [color=blue]
              > John G Harris wrote:
              >[color=green]
              >> In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
              >> Lahn <PointedEars@we b.de> writes[/color][/color]

              [snip]
              [color=blue][color=green][color=darkred]
              >>> Nested functions are not allowed in ECMAScript and implementations .
              >>> However, it is supported in JavaScript 1.5.[/color]
              >>
              >> It is allowed in ECMAScript v3, which has been around for over four
              >> years.[/color]
              >
              > I'd be happy to be wrong here and read about the required productions
              > from the ECMAScript 3 grammar in a posting of you.[/color]

              Will I do?

              Unless I'm missing something major (quite possible as I don't have this
              thread in full), you seem to be suggesting that

              function a() {
              function b() {
              }
              }

              is illegal. Is that correct? Well, in a code example, section 13.2 -
              Creating Function Objects, they have written almost precisely the above.

              If you want the associated grammar:

              FunctionDeclara tion :
              function Identifier ( FormalParameter Listopt ) { FunctionBody }

              FunctionBody :
              SourceElements

              SourceElements :
              SourceElement
              SourceElements SourceElement

              SourceElement :
              Statement
              FunctionDeclara tion

              That suggests to me that it is perfectly legal to nest functions to any
              degree. I haven't checked to any great extent to see if the text notes
              limitations or exceptions, but I doubt there are any.

              One thing I had noticed in the grammar is that you may not place function
              declarations directly within a statement. As far as I can see, function
              statements may only occur in:

              - The program source at the "root" level.
              - Other functions (expressions or statements).
              - An eval() argument.

              That would make something like:

              if(...) {
              function myFunction() {
              }
              }

              illegal. However, a quick test shows that (at least some) browsers support
              it. Why you'd do something like this, though, is another question.

              Mike

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              • Richard Cornford

                #8
                Re: ECMAScript FunctionDeclara tion Grammar (was: Re: Why wont this script work?)

                Michael Winter wrote:[color=blue]
                > On Sun, 17 Oct 2004 23:26:27 +0200, Thomas 'PointedEars' Lahn
                > <PointedEars@we b.de> wrote:[color=green]
                >> John G Harris wrote:[color=darkred]
                >>> In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas
                >>> 'PointedEars' Lahn <PointedEars@we b.de> writes[/color][/color][/color]
                <snip>[color=blue][color=green][color=darkred]
                >>>> Nested functions are not allowed in ECMAScript and
                >>>> implementations . However, it is supported in JavaScript 1.5.[/color][/color][/color]

                Nested function declarations and function expressions are supported by
                Netscape 4 with JavaScript 1.3 and IE 4 (approx: JScript 2), about the
                oldest browsers that remain viable as scriptable Internet user agents
                (certainly the oldest considered for active support in a commercial
                context, and often not then).

                As is not unusual for such documents, the 3rd edition of ECMAScript was
                formalising behaviour already common to implementations .
                [color=blue][color=green][color=darkred]
                >>> It is allowed in ECMAScript v3, which has been around for
                >>> over four years.[/color][/color][/color]

                And has been a standard feature of browser scripting engine
                implementations considerably longer.
                [color=blue][color=green]
                >> I'd be happy to be wrong here and read about the required
                >> productions from the ECMAScript 3 grammar in a posting of you.[/color]
                >
                > Will I do?
                >
                > Unless I'm missing something major (quite possible as I don't
                > have this thread in full), you seem to be suggesting that
                >
                > function a() {
                > function b() {
                > }
                > }
                >
                > is illegal. Is that correct? Well, in a code example, section
                > 13.2 - Creating Function Objects, they have written almost
                > precisely the above.[/color]

                If nested functions were not explicitly supported there would not be
                much point in ECMA 262 (3rd edition) going into quite so much detail
                about how the scope chain and the internal [[Scope]] properties are
                handled, as there would be no ECMAScript circumstances where it would
                make any difference (which is probably why the second edition can get
                away with defining only one form of scope chain for any execution
                context).
                [color=blue]
                > If you want the associated grammar:
                >
                > FunctionDeclara tion :
                > function Identifier ( FormalParameter Listopt ) { FunctionBody }
                >
                > FunctionBody :
                > SourceElements
                >
                > SourceElements :
                > SourceElement
                > SourceElements SourceElement
                >
                > SourceElement :
                > Statement
                > FunctionDeclara tion[/color]

                Yes, function bodies may explicitly contain function declarations, and
                function expressions may appear anywhere that a MemberExpressio n can be
                used:-

                | 11.2 Left-Hand-Side Expressions
                | Syntax
                | MemberExpressio n :
                | PrimaryExpressi on
                | FunctionExpress ion
                | MemberExpressio n [ Expression ]
                | MemberExpressio n . Identifier
                | new MemberExpressio n Arguments

                [color=blue]
                > That suggests to me that it is perfectly legal to nest functions
                > to any degree. I haven't checked to any great extent to see if
                > the text notes limitations or exceptions, but I doubt there are
                > any.
                >
                > One thing I had noticed in the grammar is that you may not place
                > function declarations directly within a statement. As far as I
                > can see, function statements may only occur in:
                >
                > - The program source at the "root" level.
                > - Other functions (expressions or statements).
                > - An eval() argument.
                >
                > That would make something like:
                >
                > if(...) {
                > function myFunction() {
                > }
                > }
                >
                > illegal. However, a quick test shows that (at least some)
                > browsers support it.[/color]

                More involved tests would show extremely inconsistent handling of that
                code. The production rules forbid a function declaration form appearing
                within a block statement so the only valid ECMAScript interpretation of
                that is as a function expression with optional identifier, and just
                asserted (never assigned to anything or called, so a pointless
                expression).

                In practice browsers may take it as a function declaration (unless
                explicitly parenthesised, which makes it unambiguously an expression)
                and act on it during variable instantiation (effectively removing it
                form its block context), and Mozilla browsers treat it as an expression
                but erroneously (by ECMA 262) leak the optional identifier into the
                containing scope (producing an effect indistinguishab le form a
                conditional function declaration (if such existed in ECMAScript).
                [color=blue]
                > Why you'd do something like this, though, is another
                > question.[/color]

                You wouldn't, but an inner function expression, conditionally evaluated
                and assigned, is completely normal.

                Richard.


                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: ECMAScript FunctionDeclara tion Grammar (was: Re: Why wont this script work?)

                  Richard Cornford wrote:
                  [color=blue]
                  > Michael Winter wrote:[color=green]
                  >> FunctionDeclara tion :
                  >> function Identifier ( FormalParameter Listopt ) { FunctionBody }
                  >>
                  >> FunctionBody :
                  >> SourceElements
                  >>
                  >> SourceElements :
                  >> SourceElement
                  >> SourceElements SourceElement
                  >>
                  >> SourceElement :
                  >> Statement
                  >> FunctionDeclara tion[/color]
                  >
                  > Yes, function bodies may explicitly contain function declarations,[/color]

                  ACK, but only at first level.
                  [color=blue]
                  > and function expressions may appear anywhere that a MemberExpressio n can
                  > be used:-
                  >
                  > | 11.2 Left-Hand-Side Expressions
                  > | Syntax
                  > | MemberExpressio n :
                  > | PrimaryExpressi on
                  > | FunctionExpress ion
                  > | MemberExpressio n [ Expression ]
                  > | MemberExpressio n . Identifier
                  > | new MemberExpressio n Arguments[/color]

                  However, the flaw of the latter argument (if that is what it is supposed
                  to be) is that a FunctionExpress ion is not a FunctionDeclara tion and
                  vice-versa.


                  PointedEars

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Why wont this script work?

                    JRS: In article <2018545.i0tZq4 expL@PointedEar s.de>, dated Sun, 17 Oct
                    2004 23:26:27, seen in news:comp.lang. javascript, Thomas 'PointedEars'
                    Lahn <PointedEars@we b.de> posted :[color=blue]
                    >John G Harris wrote:
                    >[color=green]
                    >> In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
                    >> Lahn <PointedEars@we b.de> writes[/color]
                    >
                    >Please stop posting attribution novels; the reasons have been explained
                    >(to you) several times before.[/color]

                    They are invalid reasons. There is no support for them in accepted
                    Usenet standards and norms; and Usefor WIP allows a full attribution
                    such as I use.

                    You may recall that the German nation got rather a bad name in the early
                    1870s, the 1910s, and in 1933-45. It seems unwise of you to attempt to
                    repeat the situation single-handedly.

                    Given your rather silly habit of reviving ancient threads, threads that
                    the saner readers of c.l.j have long lost interest in, threads that are
                    no longer conveniently accessible to those using off-line newsreaders,
                    putting the date of the previous article in an attribution would be a
                    convenience to users of the group -- it is not always possible to
                    determine the date of a previous article from its message-ID in
                    References, and, when it is possible, many will not know how to do it,
                    and others find it inconvenient to understand.

                    In brief, pleas cease your Schickelgrubing .

                    [color=blue][color=green][color=darkred]
                    >>> Nested functions are not allowed in ECMAScript and implementations .
                    >>> However, it is supported in JavaScript 1.5.[/color]
                    >>
                    >> It is allowed in ECMAScript v3, which has been around for over four
                    >> years.[/color]
                    >
                    >I'd be happy to be wrong here and read about the required productions
                    >from the ECMAScript 3 grammar in a posting of you.[/color]

                    They are supported in MSIE4, which Flanagan says is JS 1.2; that is over
                    six years old.


                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                    Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                    Comment

                    • Dr John Stockton

                      #11
                      Re: ECMAScript FunctionDeclara tion Grammar (was: Re: Why wont this script work?)

                      JRS: In article <opsf1ccriyx13k vk@atlantis>, dated Sun, 17 Oct 2004
                      22:19:29, seen in news:comp.lang. javascript, Michael Winter <M.Winter@bl
                      ueyonder.co.inv alid> posted :[color=blue]
                      >
                      >That would make something like:
                      >
                      > if(...) {
                      > function myFunction() {
                      > }
                      > }
                      >
                      >illegal. However, a quick test shows that (at least some) browsers support
                      >it. Why you'd do something like this, though, is another question.[/color]

                      My IE4 allows it, but does not support it.

                      In other words, the condition is ignored, and the new function is always
                      defined.

                      It's easy to see that one might well want to replace a function that is
                      already defined, and that looks a reasonable way of doing it. There are
                      probably other ways, though, so that one is not necessary.


                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • Michael Winter

                        #12
                        Re: ECMAScript FunctionDeclara tion Grammar (was: Re: Why wont this script work?)

                        On Mon, 18 Oct 2004 14:07:37 +0100, Dr John Stockton
                        <spam@merlyn.de mon.co.uk> wrote:
                        [color=blue]
                        > JRS: In article <opsf1ccriyx13k vk@atlantis>, dated Sun, 17 Oct 2004
                        > 22:19:29, seen in news:comp.lang. javascript, Michael Winter
                        > <M.Winter@bluey onder.co.invali d> posted :
                        >[color=green]
                        >> That would make something like:
                        >>
                        >> if(...) {
                        >> function myFunction() {
                        >> }
                        >> }[/color][/color]

                        [snip]
                        [color=blue]
                        > It's easy to see that one might well want to replace a function that is
                        > already defined,[/color]

                        Of course. A self-configuring script could use it to replace a
                        decision-making function with a single code path. Initially, the function
                        tests the environment, and if it's sure that the browser will be
                        consistent, it could replace itself with code that uses a particular
                        feature set rather than selecting on each invocation, increasing the
                        performance of subsequent executions. I believe Richard used that in his
                        Russian Doll pattern demonstration, and I have, too (though to a simpler
                        extent).
                        [color=blue]
                        > and that looks a reasonable way of doing it. There are probably other
                        > ways, though, so that one is not necessary.[/color]

                        The correct way would be:

                        var functionName = function( argumentList ) {
                        };

                        that is, a function expression. As function declarations are evaluated
                        when the relevant scope is entered, I'd imagine[1] that the last function
                        would always be the one referenced by the identifier, irrespective of
                        whatever conditional statements might surround it. That was why I asked
                        what the point would be.

                        Mike


                        [1] Read: untested. As it's not legal, so something I wouldn't do, I
                        haven't given it much attention. As Richard hinted, implementations will
                        probably vary anyway, so best avoid it. As you supposed: there are other
                        ways.

                        --
                        Michael Winter
                        Replace ".invalid" with ".uk" to reply by e-mail.

                        Comment

                        • John G Harris

                          #13
                          Re: Why wont this script work?

                          In article <2018545.i0tZq4 expL@PointedEar s.de>, Thomas 'PointedEars'
                          Lahn <PointedEars@we b.de> writes[color=blue]
                          >John G Harris wrote:
                          >[color=green]
                          >> In article <1369832.dfOYAL PPKV@PointedEar s.de>, Thomas 'PointedEars'
                          >> Lahn <PointedEars@we b.de> writes[/color]
                          >
                          >Please stop posting attribution novels; the reasons have been explained
                          >(to you) several times before.[/color]

                          It isn't, and you haven't.

                          [color=blue][color=green][color=darkred]
                          >>> Nested functions are not allowed in ECMAScript and implementations .
                          >>> However, it is supported in JavaScript 1.5.[/color]
                          >>
                          >> It is allowed in ECMAScript v3, which has been around for over four
                          >> years.[/color]
                          >
                          >I'd be happy to be wrong here and read about the required productions
                          >from the ECMAScript 3 grammar in a posting of you.[/color]

                          I'm amazed that you can make such a dogmatic statement about ECMAScript
                          without being able to look it up.

                          You must read ECMA 262, v3, sections 13 and 14.

                          John
                          --
                          John Harris

                          Comment

                          Working...