New in JavaScript explain

New in JavaScript explain

Many beginners became confused about new operators in JavaScript as many tend to think JavaScript is not Object Oriented Programing. So to make it understand better we will explore and answer three questions.

What is "new"?
How does it work behind the scenes?
What problem does it solve?
When is it appropriate and when not?


What is "new"?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that have a constructor function.

More simple way, new creates an empty object and assign properties to it and bind 'this' to it.


How does it work behind the scenes?
Well, new does below things behind the scene

  1. First, it creates an empty object
    it creates an empty object like this:
    let res = {};
    
  2. It sets this new object's internal, inaccessible, [[prototype]] (i.e. proto) property to be the constructor function's external, accessible, prototype object.
    Every object in javascript has prototypes that are not accessible and created at object creation type but what "new" does here is that it creates new property and binds it to the empty object that is created and is accessible. So in more simple terms, new adds a property to the new object(res) with the help of proto the links to the constructor functions prototype
    Object.res.__proto__ = func.prototype;
    

  3. "new" bind "this" with the newly created object
    We know the most confusing part of javascript is this. "new" binds this to the newly created object(res). so we can see the this to point the new created object
  4. It returns the newly created object unless the constructor function returns a non-null object reference.
    if the constructor function returns null then it will return object reference. if the constructor function returns non-null then it will return the appropriate result So if we need to replicate new as a function the whole code will look like this:
    function nouveau(func) {
     var res = {};
     if (func.prototype !== null) {
         res.__proto__ = func.prototype;//__proto__ is a property, in every JavaScript object, which  points to object 's constructor function's prototype property
     }
     var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
     if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
         return ret;
     }
     return res;
    }
    


    What problem does it solve?
    We can turn a function into an object and use it multiple times by the use of "new". It allows us to save too much time and unnecessary code



    When is it appropriate and when not?
    The most appropriate time of using new is when we want to use a function-like object and want to use it for new data over and over again. It saves a lot of time and lots of writing code. We have no need if the function is not going to be used more than once as it is seemed completely unnecessary



    Even I had some trouble understanding new operator in javascript for the first time. If you want more details info and want to understand better than you can visit these pages:

Well, you might find this blog much not informative or lacking or some grammatical problem. Please do not mind as it is my first blog. But if you have any suggestion for improvements or find any mistake in my understanding, you are welcome to help me improve by making a comment