473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Oct 4 '08 #1
6 1329
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
Oct 4 '08 #2
Egbert Teeselink <sk******@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:
http://joost.zeekat.nl/constructors-...confusing.html

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Oct 4 '08 #3
On Sat, 04 Oct 2008 16:53:37 +0200, Thomas 'PointedEars' Lahn
<Po*********@web.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
Oct 5 '08 #4
"Egbert Teeselink" <e.*****************@student.banana.tue.nlwrites :
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/
Oct 5 '08 #5
Egbert Teeselink wrote:
On Sat, 04 Oct 2008 16:53:37 +0200, Thomas 'PointedEars' Lahn
<Po*********@web.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
Oct 5 '08 #6
On Sat, 04 Oct 2008 16:44:37 +0200, Egbert Teeselink <sk******@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
Oct 5 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.