Skip to content

OrderNot attempted

What new Foo() does

Order the steps JavaScript performs for new Foo(args).

Drag the rows, or use the arrows, to put them in order from first to last.

  1. Return the new object, unless Foo returned an object of its own
  2. Call Foo with this bound to the new object
  3. Link its [[Prototype]] to Foo.prototype
  4. Create a brand-new empty object
Show answer & explanation

Correct order:

  1. Create a brand-new empty object
  2. Link its [[Prototype]] to Foo.prototype
  3. Call Foo with this bound to the new object
  4. Return the new object, unless Foo returned an object of its own

This is why methods put on Foo.prototype are shared, why forgetting new puts properties in the wrong place, and why a constructor that returns an object "hijacks" new.