The new operator

  • Thread starter Egbert Teeselink
  • Start date
E

Egbert Teeselink

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
 
T

Thomas 'PointedEars' Lahn

Egbert said:
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
 
J

Joost Diepenmaat

Egbert Teeselink said:
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.

<new> doesn'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 <this> set to the new object.

this may be helpful:
http://joost.zeekat.nl/constructors-considered-mildly-confusing.html
 
J

Joost Diepenmaat

Egbert Teeselink said:
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.
 
T

Thomas 'PointedEars' Lahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top