Reusing class name as variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Johannes Rusch

    Reusing class name as variable name


    Is the following code valid and supported by current implementations ?

    function somename() {
    this.show = function () { document.write( "somename called") }
    }

    var somename = new somename();
    somename.show()

    Note that the class name "somename" is reused for the variable name.

    --
    Klaus Johannes Rusch
    KlausRusch@atme dia.net

  • Vincent van Beveren

    #2
    Re: Reusing class name as variable name

    No, its invalid, because JavaScript does not have classes, only objects.
    A 'function myclass()' would create a function (which is an object)
    myclass capable of building and returning objects.

    You can see this by attempting to do the following after your code.

    someother = new somename();
    someother.show( );

    would result in an error, because it would attempt to build an object
    from something that is now another object.




    Klaus Johannes Rusch wrote:
    [color=blue]
    >
    > Is the following code valid and supported by current implementations ?
    >
    > function somename() {
    > this.show = function () { document.write( "somename called") }
    > }
    >
    > var somename = new somename();
    > somename.show()
    >
    > Note that the class name "somename" is reused for the variable name.
    >[/color]

    Comment

    • Martin Honnen

      #3
      Re: Reusing class name as variable name



      Klaus Johannes Rusch wrote:
      [color=blue]
      > Is the following code valid and supported by current implementations ?
      >
      > function somename() {
      > this.show = function () { document.write( "somename called") }
      > }
      >
      > var somename = new somename();
      > somename.show()
      >
      > Note that the class name "somename" is reused for the variable name.[/color]

      You can do the above but after the statement
      var somename = new somename();
      has been executed there no longer is a global function of the name
      'somename' thus any further
      new somename()
      will fail with an error.

      --

      Martin Honnen


      Comment

      • Klaus Johannes Rusch

        #4
        Re: Reusing class name as variable name

        Vincent van Beveren wrote:
        [color=blue]
        > No, its invalid, because JavaScript does not have classes, only objects.
        > A 'function myclass()' would create a function (which is an object)
        > myclass capable of building and returning objects.
        >
        > You can see this by attempting to do the following after your code.
        >
        > someother = new somename();
        > someother.show( );
        >
        > would result in an error, because it would attempt to build an object
        > from something that is now another object.[/color]

        Surely this would fail, the question is if reusing the name of the
        function that allows creating an object of that type (which is what I
        meant by "class" ;-)) when assigning this to a variable once and only once.

        I cannot think of a reason why this should not work but would like to
        solicit opinions before actually using this.

        --
        Klaus Johannes Rusch
        KlausRusch@atme dia.net

        Comment

        • Michael Winter

          #5
          Re: Reusing class name as variable name

          On Mon, 19 Apr 2004 16:11:49 +0200, Vincent van Beveren
          <vincent@provid ent.remove.this .nl> wrote:

          [fixed top-post]
          [color=blue]
          > Klaus Johannes Rusch wrote:
          >[color=green]
          >> Is the following code valid and supported by current implementations ?[/color][/color]
          [color=blue]
          > No, its invalid, [...][/color]

          "Invalid" is too strong, and incorrect. Unwise might be better, but it
          depends on what the OP is trying to accomplish.

          To the OP: If you are trying to create an object via an anonymous function
          (that is, make the constructor unavailable), you might want to try:

          var somename = new ( function() {
          /* constructor code */

          this.show = function {
          /* your code */
          };
          });

          /* ... */

          somename.show() ;

          Alternatively,

          var somename = ( function() {
          function myObject() {
          /* constructor code */
          }
          myObject.show = function() {
          /* your code */
          };
          return myObject;
          })();

          or, in a slight variation of the above:

          var somename = ( function() {
          return({
          show:function() {
          /* your code */
          }
          });
          })();

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Richard Cornford

            #6
            Re: Reusing class name as variable name

            Klaus Johannes Rusch wrote:[color=blue]
            > Is the following code valid and supported by current implementations ?[/color]

            Yes, but the chances that you actually want to do this are close to
            zero.

            The execution of global code commences with "variable instantiation"
            using the global object as the Activation/Variable object in the global
            execution context.

            First function declarations are evaluated and named properties created
            on the Variable object with names corresponding to the function names
            used and then references to the function objects created using the
            function definitions are assigned to those named properties.
            [color=blue]
            > function somename() {
            > this.show = function () { document.write( "somename called") }
            > }[/color]

            So at this stage the global object has a property named "somename" that
            refers to a function object.

            The next stage in variable instantiation is to create named properties
            of the Variable object for each declared (with the - var - keyword)
            local variable (local variables in global code become global variables).
            However, if the Variable object already has a named property
            corresponding with the identifier used in a variable declaration no new
            property is created and its original value is unaltered. So - var
            somename - does not result in the creation of a new property of the
            Variable object, and the existing "somename" property of that object
            continues to refer to the declared function.

            That is all of the variable instantiation required by this code so
            execution of the global code will follow.

            The first statment:-
            [color=blue]
            > var somename = new somename();[/color]

            - resolves the right had side first, the identifier - somename - is
            subject to resolutin against the scope chain, which includes the
            Variable object for the global execution context so - somename - is
            resolved as a property of that object, the property referring to the
            function object created with the function declaration.

            The - new - operator calls that funciton as a constructor and a
            reference to an object is returned. So the right hand side of the
            assignment expression evaluates as a reference to an object.

            Next the assignment is performed. To know where to assign the value the
            left hand side the identifier - somename - is scope chain resolved,
            again as a reference to a named property of the Variable object. And the
            reference to an object that was the evaluated result of the right hand
            side expression is assigned to that named property of the Variable
            object. Replacing the reference to the function object that had
            previously been the value of that property.

            As a result any further attempts to use the identifier - somename - in a
            function or constructor context will result in an error as it now refers
            to an object.
            [color=blue]
            > somename.show()
            >
            > Note that the class name "somename" is reused for the variable name.[/color]

            But only one named property of the Variable object for the global
            execution context is created, and that property can only hold one value
            at a time.

            Richard.


            Comment

            • Vincent van Beveren

              #7
              Re: Reusing class name as variable name

              Well, maybe not invalid, but I can't really think of any circumstances
              this would really be useful. It won't make things more clear, thats for
              sure. The only reason might be obfuscation and making things
              unreachable.

              Comment

              • Michael Winter

                #8
                Re: Reusing class name as variable name

                On Tue, 20 Apr 2004 16:43:40 +0200, Vincent van Beveren
                <vincent@provid ent.remove.this .nl> wrote:
                [color=blue]
                > Well, maybe not invalid, but I can't really think of any circumstances
                > this would really be useful. It won't make things more clear, thats for
                > sure. The only reason might be obfuscation and making things
                > unreachable.[/color]

                The OP e-mailed me and said the goal was to make the constructor
                unavailable once the object had been initialised. A one-off constructor.
                He decided to use one of the patterns I had shown in my post.

                Mike


                Please make sure you quote the relevant part of the post you're responding
                to. Others might not know to whom you were responding. If it was me,
                either your newsreader's broken, or you didn't actually reply to my post.

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                Working...