Overriding a constructor

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

    Overriding a constructor

    I'm trying to solve a problem with my code but can't come up with a
    solution.
    Suppose I have a "class" A with its constructor (that I can't modify
    directly)

    function A() {
    /* constructor code */
    };


    what I'd like to do is to make the constructor call a certain function I
    specify before returning, i.e.

    function A() {
    /* constructor code */
    someFunction();
    };

    is this possible?
    CAFxX
  • CAFxX

    #2
    Re: Overriding a constructor

    Just to clarify, the aim is that all the code that is instantiating an
    instance of "class" A, perform the call to someFunction() without rewrites.

    CAFxX ha scritto:
    I'm trying to solve a problem with my code but can't come up with a
    solution.
    Suppose I have a "class" A with its constructor (that I can't modify
    directly)
    >
    function A() {
    /* constructor code */
    };
    >
    >
    what I'd like to do is to make the constructor call a certain function I
    specify before returning, i.e.
    >
    function A() {
    /* constructor code */
    someFunction();
    };
    >
    is this possible?
    CAFxX

    Comment

    • sheldonlg

      #3
      Re: Overriding a constructor

      CAFxX wrote:
      I'm trying to solve a problem with my code but can't come up with a
      solution.
      Suppose I have a "class" A with its constructor (that I can't modify
      directly)
      >
      function A() {
      /* constructor code */
      };
      >
      >
      what I'd like to do is to make the constructor call a certain function I
      specify before returning, i.e.
      >
      function A() {
      /* constructor code */
      someFunction();
      };
      >
      is this possible?
      CAFxX
      Why not have a second class that extends the first class. In the second
      class you could have a method A and in its constructor you could have
      $this->A();

      Comment

      • sheldonlg

        #4
        Re: Overriding a constructor

        sheldonlg wrote:
        CAFxX wrote:
        >I'm trying to solve a problem with my code but can't come up with a
        >solution.
        >Suppose I have a "class" A with its constructor (that I can't modify
        >directly)
        >>
        > function A() {
        > /* constructor code */
        > };
        >>
        >>
        >what I'd like to do is to make the constructor call a certain function
        >I specify before returning, i.e.
        >>
        > function A() {
        > /* constructor code */
        > someFunction();
        > };
        >>
        >is this possible?
        >CAFxX
        >
        Why not have a second class that extends the first class. In the second
        class you could have a method A and in its constructor you could have
        $this->A();
        Oops, I thought I was in a different newsgroup. I don't know about
        classes in javascript.

        Comment

        • Joost Diepenmaat

          #5
          Re: Overriding a constructor

          CAFxX <usenet0001@REM OVEMEcafxx.cjb. netwrites:
          I'm trying to solve a problem with my code but can't come up with a
          solution.
          Suppose I have a "class" A with its constructor (that I can't modify
          directly)
          >
          function A() {
          /* constructor code */
          };
          >
          >
          what I'd like to do is to make the constructor call a certain function
          I specify before returning, i.e.
          >
          function A() {
          /* constructor code */
          someFunction();
          };
          >
          is this possible?
          CAFxX
          You mean you want to replace the constructor A at runtime with some other
          function that calls the original A and then adds some more
          functionality?

          var OldA = A;
          A = function() {
          OldA.apply(this ,arguments);
          // do new stuff here
          };

          // copy any properties set on OldA
          for (p in OldA) {
          if (OldA.hasOwnPro perty(p)) {
          A[p] = OldA[p];
          }
          // this may fix some potential issues
          // and break some others
          A.prototype.con structor = A;
          }

          If the original code tries hard enough, it can always detect that (and
          break because) the constructor been replaced, and if there are already
          instances of A around at the time the new constructor is created those
          instances will be instanceOf OldA, not A.

          see also:



          HTH,
          Joost.

          --
          Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

          Comment

          • Gregor Kofler

            #6
            Re: Overriding a constructor

            sheldonlg meinte:
            Oops, I thought I was in a different newsgroup. I don't know about
            classes in javascript.
            No one knows.

            Gregor


            --
            http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
            http://web.gregorkofler.com ::: meine JS-Spielwiese
            http://www.image2d.com ::: Bildagentur für den alpinen Raum

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Overriding a constructor

              Gregor Kofler wrote:
              sheldonlg meinte:
              >Oops, I thought I was in a different newsgroup. I don't know about
              >classes in javascript.
              >
              No one knows.
              That depends on what is meant by "javascript " ;-)


              SCNR

              PointedEars
              --
              Prototype.js was written by people who don't know javascript for people
              who don't know javascript. People who don't know javascript are not
              the best source of advice on designing systems that use javascript.
              -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

              Comment

              • CAFxX

                #8
                Re: Overriding a constructor

                Right, sarcasm never dies.
                I am _obviously_ talking about the prototype mechanism (notice the
                quotes around "class" in the first post).

                Gregor Kofler ha scritto:
                sheldonlg meinte:
                >
                >Oops, I thought I was in a different newsgroup. I don't know about
                >classes in javascript.
                >
                No one knows.
                >
                Gregor
                >
                >

                Comment

                • CAFxX

                  #9
                  Re: Overriding a constructor

                  hmmm, I don't get what OldA.apply is supposed to be...

                  Joost Diepenmaat ha scritto:
                  CAFxX <usenet0001@REM OVEMEcafxx.cjb. netwrites:
                  >
                  >I'm trying to solve a problem with my code but can't come up with a
                  >solution.
                  >Suppose I have a "class" A with its constructor (that I can't modify
                  >directly)
                  >>
                  > function A() {
                  > /* constructor code */
                  > };
                  >>
                  >>
                  >what I'd like to do is to make the constructor call a certain function
                  >I specify before returning, i.e.
                  >>
                  > function A() {
                  > /* constructor code */
                  > someFunction();
                  > };
                  >>
                  >is this possible?
                  >CAFxX
                  >
                  You mean you want to replace the constructor A at runtime with some other
                  function that calls the original A and then adds some more
                  functionality?
                  >
                  var OldA = A;
                  A = function() {
                  OldA.apply(this ,arguments);
                  // do new stuff here
                  };
                  >
                  // copy any properties set on OldA
                  for (p in OldA) {
                  if (OldA.hasOwnPro perty(p)) {
                  A[p] = OldA[p];
                  }
                  // this may fix some potential issues
                  // and break some others
                  A.prototype.con structor = A;
                  }
                  >
                  If the original code tries hard enough, it can always detect that (and
                  break because) the constructor been replaced, and if there are already
                  instances of A around at the time the new constructor is created those
                  instances will be instanceOf OldA, not A.
                  >
                  see also:
                  >

                  >
                  HTH,
                  Joost.
                  >

                  Comment

                  • Joost Diepenmaat

                    #10
                    Re: Overriding a constructor

                    CAFxX <usenet0001@REM OVEMEcafxx.cjb. netwrites:
                    hmmm, I don't get what OldA.apply is supposed to be...
                    somefunction.ap ply() calls the function passing a specific "this" and
                    arguments.

                    See, for example:

                    The apply() method of Function instances calls this function with a given this value, and arguments provided as an array (or an array-like object).



                    --
                    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                    Comment

                    • Gregor Kofler

                      #11
                      Re: Overriding a constructor

                      CAFxX meinte:
                      Right, sarcasm never dies.
                      I am _obviously_ talking about the prototype mechanism (notice the
                      quotes around "class" in the first post).
                      I noticed and I wasn't replying to you.

                      Gregor


                      --
                      http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                      http://web.gregorkofler.com ::: meine JS-Spielwiese
                      http://www.image2d.com ::: Bildagentur für den alpinen Raum

                      Comment

                      Working...