0

I was learning about constructors, and I came across the new keyword.

var obj = new myContructor();

I learnt that it created a new object, set its prototype as constructor.prototype, set its properties as according with the this keyword, and finally returns that object.

Now, I am confused as to where exactly does it create the new object, as inside memory or somewhere where it is volatile.

And what do we mean when saying it RETURNS that object, that it creates a copy of the new object at the location of the var obj, or does it reference obj to wherever it created the new object ?

Jai
  • 71,335
  • 12
  • 70
  • 93
Dhruv Chadha
  • 919
  • 7
  • 25
  • ThisBinding is something that the JavaScript interpreter maintains as it evaluates JavaScript code, like a special CPU register which holds a reference to an object. The interpreter updates the ThisBinding whenever establishing an execution context in one of only three different cases. Check [THIS] (http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) link. – Shubham Jul 08 '16 at 08:49
  • I am closing this as too broad, although this can be duplicate of some posts. At MDN you can find it explained – Jai Jul 08 '16 at 08:49
  • The object is created on the heap managed by the JavaScript runtime. A number corresponding to a memory location in the heap is returned (a reference) because objects are passed by "value of reference". This number (aka "reference") is copied around. Copying the whole object would be prohibitively expensive and is not what you normally want anyway. – Ben Aston Jul 08 '16 at 08:52

2 Answers2

2

inside memory or somewhere where it is volatile

Yes, of course. Just like any other piece of data in the program.

do we mean when saying it RETURNS that object

You are making a function call. Function calls have return values. The object is the return value of that function call.

it creates a copy of the new object at the location of the var obj

It creates it in the function, then it returns a reference to it (just like any other object), and that reference is stored in the variable because you used an assignment.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 1
    Mumble mumble nitpick: the object isn't directly the return value of the function call (unless it is, of course). It's really the result of the `new` operator... :o) – deceze Jul 08 '16 at 08:52
  • What do you mean by 'creates the object INSIDE the function' ? – Dhruv Chadha Jul 09 '16 at 05:46
1

There are many things going in behind the scenes in Javascript; things are constantly being created in memory that you do not have access to. As far as you are concerned with this while writing Javascript code:

  1. new creates an object and does all the prototype stuff
  2. myConstructor() is executed, and this inside the function is set to that object
    (put another way: myContructor is called with the object as context)
  3. after myConstructor is done, the object is assigned to the variable obj as result of all this

There are a bunch of caveats regarding what myConstructor can return and how that influences the result, but we'll ignore that for simplicity. The basic chain of events is new creates object → myConstructor sees that object as thisobj "receives" this object as return value of new.

Of course, all this creating of the object and passing it around to different places is done by the Javascript engine, and requires internal storage of the object somewhere in memory.

deceze
  • 471,072
  • 76
  • 664
  • 811