JavaScript constructor syntacs

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

    JavaScript constructor syntacs

    I never managed to ask before:

    Why this backward way is so often used for constructors:

    myConstructor = function() { /*empty placeholder*/ }
    // and then:
    myConstructor.p rototype.method One = function() {...}
    myConstructor.p rototype.method Two = function() {...}

    instead of right away:

    function myConstructor() {
    this.methodOne = function() {...}
    this.methodTwo = function() {...}
    }

    Is it just a tradition or is it a workaround of some problem I'm not
    aware of?

  • Julian Turner

    #2
    Re: JavaScript constructor syntacs


    VK wrote:
    [color=blue]
    > I never managed to ask before:
    >
    > Why this backward way is so often used for constructors:
    >
    > myConstructor = function() { /*empty placeholder*/ }
    > // and then:
    > myConstructor.p rototype.method One = function() {...}
    > myConstructor.p rototype.method Two = function() {...}
    >
    > instead of right away:
    >
    > function myConstructor() {
    > this.methodOne = function() {...}
    > this.methodTwo = function() {...}
    > }
    >
    > Is it just a tradition or is it a workaround of some problem I'm not
    > aware of?[/color]

    The simple difference between the two is memory efficiency.

    Using prototype creates each function only once, when the constructor
    is created.

    Including the function within the constructor, means that a new copy of
    the function is created in memory each time you call the constructor to
    create an instance.

    Of course that is not to say that the second method is in any way
    invalid, and indeed the second method is needed if you want to create
    public functions which are unique to each instance of your object.

    Julian

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: JavaScript constructor syntacs

      Julian Turner wrote:
      [color=blue]
      > VK wrote:[color=green]
      >> Why this backward way is so often used for constructors:[/color][/color]

      It is not a backward way.
      [color=blue][color=green]
      >> myConstructor = function() { /*empty placeholder*/ }[/color][/color]

      I prefer

      function MyConstructor(. ..)
      {
      // ...
      }
      [color=blue][color=green]
      >> // and then:
      >> myConstructor.p rototype.method One = function() {...}
      >> myConstructor.p rototype.method Two = function() {...}[/color][/color]

      Or using a method to add properties to the prototype.
      [color=blue][color=green]
      >> instead of right away:
      >>
      >> function myConstructor() {
      >> this.methodOne = function() {...}
      >> this.methodTwo = function() {...}
      >> }
      >>
      >> Is it just a tradition or is it a workaround of some problem
      >> I'm not aware of?[/color][/color]

      Neither one.
      [color=blue]
      > The simple difference between the two is memory efficiency.[/color]

      Not only that.
      [color=blue]
      > Using prototype creates each function only once,[/color]

      Which means that every object created with this constructor and every
      object derived from the prototype automatically has this method. If
      one is to use the prototype chain for inheritance, only prototype
      properties are inherited through it.
      [color=blue]
      > when the constructor is created.[/color]

      You mean _called_. The constructor is "created" only once,
      through definition and during initial variable instantiation.
      [color=blue]
      > Including the function within the constructor, means that a new copy of
      > the function is created in memory each time you call the constructor to
      > create an instance.
      >
      > Of course that is not to say that the second method is in any way
      > invalid, and indeed the second method is needed if you want to create
      > public functions which are unique to each instance of your object.[/color]

      True.


      PointedEars

      Comment

      • Julian Turner

        #4
        Re: JavaScript constructor syntacs


        Thomas 'PointedEars' Lahn wrote:

        [snip][color=blue][color=green]
        > > when the constructor is created.[/color]
        >
        > You mean _called_. The constructor is "created" only once,
        > through definition and during initial variable instantiation.[/color]

        Sorry, just for my better understanding and education:-

        If you mean by _called_, the evaluation of the the assignment
        expressions, by which function expressions are evaluated and assigned
        to properties of the prototype, after definition and variable
        instantiation, then yes. I.e. the point at which the executing code
        (be it global or function code) first encounters and evaluates the
        expression:-

        MyObject.protot ype.myMethod=fu nction(){

        };

        The point being, that it is the evaluation of this expression that
        creates the function.

        If you mean by _called_, the point at which you use the constructor,
        then no. I.e.

        var myInstance=new Constructor();

        Unless I have misunderstood something (quite likely) at this point the
        prototype functions are already in place in memory.

        Regards

        Julian

        Comment

        • VK

          #5
          Re: JavaScript constructor syntacs

          Julian,

          I suggest do not jump on it: you cannot win in an unmoderated newsgroup
          against a professional troll. Just let him go. I just put recently the
          filter on P.E. and life got much better (naturally anyone is welcome to
          do the same on me).

          I would never notice anything but I sew you answering "to the space" :-)

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: JavaScript constructor syntacs

            Julian Turner wrote:
            [color=blue]
            > Thomas 'PointedEars' Lahn wrote:
            > [snip][/color]

            It was probably not a good idea to remove context here.
            [color=blue]
            > [...]
            > Unless I have misunderstood something (quite likely) at this point the
            > prototype functions are already in place in memory.[/color]

            Probably we have a mutual misunderstandin g. Say, for example, that the
            constructor is the following method:

            function MyObject(x)
            {
            this.foo = function(y)
            {
            return x;
            }
            }

            then objects created through

            var newObject = new MyObject(...);

            have a `foo' method that is unique to an MyObject object, regarding that
            there is not the same method for other MyObject objects unless the
            conditions for creation (here: the value passed to the constructor) are
            the same. If that method is modified, other MyObject objects and objects
            derived from MyObject are not affected.

            If the constructor and prototype are instead defined as

            function MyObject(x)
            {
            }

            MyObject.protot ype.foo = function(y)
            {
            return x;
            }

            then MyObject objects would also have `foo' method, however a) its return
            value depends solely on the value of `x' when it is _called_ and b) all
            MyObject objects would share this method since it is accessed through the
            prototype chain. Modifying that method would mean that it is modified for
            all MyObject objects as well as for all objects derived from MyObject,
            unless overwritten there.

            That prototype method is not created when the constructor is called (with
            `new') but right after the latter was instantiated (provided there is no
            code between that forces instantiation).

            Did you mean that in the first place?


            PointedEars

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: JavaScript constructor syntacs

              VK wrote:
              [color=blue]
              > I suggest do not jump on it: you cannot win in an unmoderated newsgroup
              > against a professional troll.[/color]

              Who is the one of us all here posting unprovable (because nonsensical)
              assumptions as the Holy Truth and refuses to see the hard evidence that
              proves the opposite? And you are calling me a professional troll? YMMD.
              If that is so, it is an honor to be called a troll by you.
              [color=blue]
              > Just let him go. I just put recently the filter on P.E. and life got
              > much better (naturally anyone is welcome to do the same on me).[/color]

              Yeah, all other people are driving on the wrong side, but not you.
              If had had time for this, I would pity you.


              PointedEars

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: JavaScript constructor syntacs

                VK wrote:
                [color=blue]
                > I suggest do not jump on it: you cannot win in an unmoderated newsgroup
                > against a professional troll.[/color]

                Who is the one of us all here posting unprovable (because nonsensical)
                assumptions as the Holy Truth and refuses to see the hard evidence that
                proves the opposite? And you are calling me a professional troll? YMMD.
                If that is so, it is an honor to be called a troll by you.
                [color=blue]
                > Just let him go. I just put recently the filter on P.E. and life got
                > much better (naturally anyone is welcome to do the same on me).[/color]

                Yeah, all other people are driving on the wrong side, but not you.
                If I had time for this, I would pity you.


                PointedEars

                Comment

                • Julian Turner

                  #9
                  Re: JavaScript constructor syntacs


                  Thomas 'PointedEars' Lahn wrote:
                  [color=blue]
                  > Julian Turner wrote:
                  >[color=green]
                  > > Thomas 'PointedEars' Lahn wrote:
                  > > [snip][/color]
                  >
                  > It was probably not a good idea to remove context here.[/color]

                  True.
                  [color=blue][color=green]
                  > > [...]
                  > > Unless I have misunderstood something (quite likely) at this point the
                  > > prototype functions are already in place in memory.[/color]
                  >
                  > Probably we have a mutual misunderstandin g.[/color]

                  I agree. On my side I think.
                  [color=blue]
                  > Say, for example, that the
                  > constructor is the following method:
                  >
                  > function MyObject(x)
                  > {
                  > this.foo = function(y)
                  > {
                  > return x;
                  > }
                  > }
                  >
                  > then objects created through
                  >
                  > var newObject = new MyObject(...);
                  >
                  > have a `foo' method that is unique to an MyObject object, regarding that
                  > there is not the same method for other MyObject objects unless the
                  > conditions for creation (here: the value passed to the constructor) are
                  > the same. If that method is modified, other MyObject objects and objects
                  > derived from MyObject are not affected.[/color]

                  Agreed.
                  [color=blue]
                  > If the constructor and prototype are instead defined as
                  >
                  > function MyObject(x)
                  > {
                  > }
                  >
                  > MyObject.protot ype.foo = function(y)
                  > {
                  > return x;
                  > }
                  >
                  > then MyObject objects would also have `foo' method, however a) its return
                  > value depends solely on the value of `x' when it is _called_ and b) all
                  > MyObject objects would share this method since it is accessed through the
                  > prototype chain. Modifying that method would mean that it is modified for
                  > all MyObject objects as well as for all objects derived from MyObject,
                  > unless overwritten there.[/color]

                  Agreed.
                  [color=blue]
                  > That prototype method is not created when the constructor is called (with
                  > `new') but right after the latter was instantiated (provided there is no
                  > code between that forces instantiation).
                  >
                  > Did you mean that in the first place?[/color]

                  Yes I did. I could have expressed myself better.

                  Thanks.

                  Julian

                  Comment

                  Working...