liveBook · Manning

New keyword

As explained in Secrets of the JavaScript Ninja, Second Edition

A JavaScript operator that creates an object instance and invokes a constructor function with that object as its this value.

A function call and a constructor call can use the same function but produce different results. Put `new` before the call when you want a new object instance initialized by that function. A constructor function is an ordinary function; how you invoke it makes the difference. section 4.2.3

What happens when you call `new Ninja()`?

  • JavaScript creates an object and passes it to `Ninja` as `this`. Assignments such as `this.skulk = function() { return this; }` give the object a method. section 4.2.3
  • The new object's prototype is set to `Ninja.prototype`. A method placed there can be called on an instance without being assigned inside the constructor. section 7.2
  • A primitive returned by the constructor does not replace the new object. An explicitly returned object does: it becomes the result instead. section 4.2.3

Two instances, one initializer

`function Ninja() { this.skulk = function() { return this; }; }` followed by `var ninja1 = new Ninja(); var ninja2 = new Ninja();` creates two objects. Calling `ninja1.skulk()` returns `ninja1`; calling `ninja2.skulk()` returns `ninja2`. section 4.2.3

Listing 7.2. Creating a new instance with a prototyped method
function Ninja(){}                                          #A
Ninja.prototype.swingSword = function(){                    #B
  return true;                                              #B
};                                                          #B

const ninja1 = Ninja();                                     #C
assert(ninja1 === undefined,                                #C
       "No instance of Ninja created.");                    #C

const ninja2 = new Ninja();                                 #D
assert(ninja2 &&                                            #D
       ninja2.swingSword &&                                 #D
       ninja2.swingSword(),                                 #D
       "Instance exists and method is callable." );         #D

Compare `Ninja()` with `new Ninja()`: the ordinary call returns `undefined`, while the constructor call creates an instance that can use a method added to `Ninja.prototype`. section 7.2

Watch the return value when a constructor seems to ignore your initialization. If it explicitly returns another object, that object wins—even if the constructor assigned properties to `this`. Also, `new Function(...)`, which creates a function from strings, is a different use of `new` from invoking your own constructor function. section 4.2.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains object creation, `this`, prototype linkage, and return-value exceptions through contrasting calls and examples.
Examples
Contrasts ordinary and constructor calls, constructs two objects with methods, and tests primitive and object returns.
What it assumes
Assumes familiarity with function calls and object properties while explaining constructor invocation and prototype linkage.
What you can do after
Build it from scratch: initialize instances with `new` and make methods available through a constructor's prototype.
How it connects
Connects constructor functions to function invocation, `this`, object instances, and prototypes.

Also explained in

Compare how 2 books explain new keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.