The new operator

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

    The new operator

    Hi guys,

    I've searched around in books and on the web, but probably not good
    enough because I haven't been able to find out what *exactly* the new
    operator does; all sites and books explain it exclusively my example.

    My underbelly feeling says that "new" creates a shallow copy of the
    referenced object and then calls its constructor function, binding
    "this" to the newly created copy, and returns the result of the
    constructor function, if any, or its "this" object.

    Does that make any sense? Is there any resource that I missed where
    this is clarified? Thanks!

    Egbert
  • Thomas 'PointedEars' Lahn

    #2
    Re: The new operator

    Egbert Teeselink wrote:
    I've searched around in books and on the web, but probably not good
    enough because I haven't been able to find out what *exactly* the new
    operator does; all sites and books explain it exclusively my example.
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
    Parse error.
    My underbelly feeling says that "new" creates a shallow copy of the
    referenced object and then calls its constructor function, binding
    "this" to the newly created copy, and returns the result of the
    constructor function, if any, or its "this" object.
    That is not what happens, of course. There is no referenced non-Function
    object to begin with.
    Does that make any sense?
    No.
    Is there any resource that I missed where this is clarified? Thanks!
    ECMAScript Language Specification, Edition 3 Final, section 11.2.2, and
    implementation sources.


    HTH

    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann

    Comment

    • Joost Diepenmaat

      #3
      Re: The new operator

      Egbert Teeselink <skrebbel@gmail .comwrites:
      Hi guys,
      >
      I've searched around in books and on the web, but probably not good
      enough because I haven't been able to find out what *exactly* the new
      operator does; all sites and books explain it exclusively my example.
      >
      My underbelly feeling says that "new" creates a shallow copy of the
      referenced object and then calls its constructor function, binding
      "this" to the newly created copy, and returns the result of the
      constructor function, if any, or its "this" object.
      <newdoesn't copy anything. it creates a new "empty" object, sets its
      prototype to the prototype property of the constructor, and then calls
      the constructor with <thisset to the new object.

      this may be helpful:


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

      Comment

      • Egbert Teeselink

        #4
        Re: The new operator

        On Sat, 04 Oct 2008 16:53:37 +0200, Thomas 'PointedEars' Lahn
        <PointedEars@we b.dewrote:
        Egbert Teeselink wrote:
        >I've searched around in books and on the web, but probably not good
        >enough because I haven't been able to find out what *exactly* the new
        >operator does; all sites and books explain it exclusively my example.
        ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
        Parse error.
        Woops, I meant "by example". And naturally not all sites and books, but
        all that I came across.
        >My underbelly feeling says that "new" creates a shallow copy of the
        >referenced object and then calls its constructor function, binding
        >"this" to the newly created copy, and returns the result of the
        >constructor function, if any, or its "this" object.
        >
        That is not what happens, of course.
        Of course not. Doh. Why did I even bother asking?
        There is no referenced non-Function
        object to begin with.
        Ahh right, I thought you could basically do "new" on anything.

        So rephrase, doing "new Something()" requires that Something is the name
        of a function, and this function will be called with an empty object bound
        to "this", right? So, it would be roughly equivalent to Something.call( {})
        ?
        >Does that make any sense?
        >
        No.
        >
        >Is there any resource that I missed where this is clarified? Thanks!
        >
        ECMAScript Language Specification, Edition 3 Final, section 11.2.2, and
        implementation sources.
        Aye, the specs. Somehow never thought of those as documentation material.

        Thanks,

        Egbert

        Comment

        • Joost Diepenmaat

          #5
          Re: The new operator

          "Egbert Teeselink" <e.nofruit.tees elink@student.b anana.tue.nlwri tes:
          Ahh right, I thought you could basically do "new" on anything.
          Just constructor calls.
          So rephrase, doing "new Something()" requires that Something is the
          name of a function, and this function will be called with an empty
          object bound to "this", right? So, it would be roughly equivalent to
          Something.call( {}) ?
          It requires that Something refers to an object that is marked as being a
          constructor. *Roughly* speaking this means you can call new on any
          function written in javascript itself, and on some host objects that are
          documented as being constructors. See the Ecma-262 specs, sections 8.6.2
          and 13.

          By the way: the name of the variable referring to the constructor is not
          relevant:

          var chain = { to: { constr: Something } };

          new chain.to.constr ();

          Will work just as well, and the resulting object cannot portably tell
          the difference.

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

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: The new operator

            Egbert Teeselink wrote:
            On Sat, 04 Oct 2008 16:53:37 +0200, Thomas 'PointedEars' Lahn
            <PointedEars@we b.dewrote:
            >Egbert Teeselink wrote:
            >There is no referenced non-Function object to begin with.
            That is not quite correct. There is no referenced non-constructable object.
            Ahh right, I thought you could basically do "new" on anything.
            >
            So rephrase, doing "new Something()" requires that Something is the name
            of a function,
            On/of a reference to a(ny) constructor, that is, a(ny) object that
            implements the internal [[Construct]] method. Function objects do this,
            several host objects (Option, Image, ActiveXObject etc.), too.
            and this function will be called with an empty object bound to "this",
            right? So, it would be roughly equivalent to Something.call( {})?
            Correct, roughly.


            PointedEars
            --
            realism: HTML 4.01 Strict
            evangelism: XHTML 1.0 Strict
            madness: XHTML 1.1 as application/xhtml+xml
            -- Bjoern Hoehrmann

            Comment

            • Egbert Teeselink

              #7
              Re: The new operator

              On Sat, 04 Oct 2008 16:44:37 +0200, Egbert Teeselink <skrebbel@gmail .com>
              wrote:
              Hi guys,
              >
              I've searched around in books and on the web, but probably not good
              enough because I haven't been able to find out what *exactly* the new
              operator does; all sites and books explain it exclusively my example.
              >
              My underbelly feeling says that "new" creates a shallow copy of the
              referenced object and then calls its constructor function, binding
              "this" to the newly created copy, and returns the result of the
              constructor function, if any, or its "this" object.
              >
              Does that make any sense? Is there any resource that I missed where
              this is clarified? Thanks!
              >
              Egbert
              Thanks, Joost and Thomas, for your answers!

              Egbert

              Comment

              Working...