new Binding

Memosha Sinha
1 min readSep 16, 2019

--

This is part of the series this keyword in Javascript

Javascript has “new operator” which may seem like new in Java.
Hmm, its not like the constructors in other languages. Then what is a constructor in JS?

  • functions called with new operator
  • not attached to any class
  • not instantiation

When function is invoked with new, behind the scene below things happen:-

  • new object is created( constructed).
  • newly constructed object is [[Prototype]] linked
  • newly constructed object is set as the this binding for that function call.
  • Unless function returns its own alternate object, the new invoked function call will automatically return the newly constructed object.
function newBinding(a){
this.a = a;
}
var newlyCreated = new newBinding(10);
console.log(newBinding.a)//10;
calling new in front of newBinding, we have constructed a new object and set that new object as the "this" for the call of newBinding.

--

--

Memosha Sinha

Software Engineer, Fitness lover, Traveller, wanderlust, optimistic