cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

F.prototype & constructors

Learn how new links objects to a constructor’s prototype, why the constructor property matters, how prototype methods are shared, and how constructor inheritance works.

By the end, you can
  • 01
    Explain F.prototypeDescribe the link new F() gives each instance and how lookup uses it.
  • 02
    Share behavior deliberatelyMove methods from per-instance copies onto a constructor’s prototype.
  • 03
    Build constructor inheritanceConnect child and parent constructors with call, Object.create, and constructor.

The template shelf behind constructor objects

The lesson titled The prototype chain showed that property lookup can climb from one object to another. This lesson answers the constructor version of the same question: when new User("Ada") creates an object, which object does Ada delegate to?

The answer is User.prototype. Every ordinary function that can be used with new owns a prototype property. That property is an object. When you call the function with new, the new instance gets a hidden [[Prototype]] link to that object.

Real-life analogyThe constructor's template shelf

Imagine a builder who makes many badges. Each badge gets its own name engraved on it, but they all use the same workshop shelf for tools like “print greeting.” Putting the tool on the shelf avoids attaching a separate copy to every badge.

In real life: A workshop builder
In JavaScript: The constructor function, such as User
In real life: A shelf of shared tools
In JavaScript: User.prototype
In real life: A finished item with its own label
In JavaScript: An instance, such as ada, with own fields
In real life: Walking back to the shelf for a tool
In JavaScript: Prototype lookup for ada.greet

Where the analogy stops: A real shelf is a place in a room. A JavaScript prototype is an object reference. The important part is sharing: many instances can point at the same object.

One-line definition

F.prototype is the object that new F() assigns as the new instance’s prototype link.

You will use that fact to share methods, understand the inherited constructor property, replace a prototype safely, and set up inheritance between constructor functions. Class syntax in the next module builds on the same links; it just writes them for you.

The constructor property is a label, not magic

INTERACTIVE

A function’s default prototype object starts with one property: constructor, pointing back to the function. That is why ada.constructor === User is true in the first experiment. Ada does not own that property. Lookup finds it on User.prototype.

Real-life analogyThe shelf label

The constructor property is like a sticker on the shelf saying which builder it belongs to. If you swap the shelf, the old sticker does not automatically move.

In real life: A label on the shelf saying “Built by User”
In JavaScript: User.prototype.constructor = User
In real life: A badge looking up at the shelf label
In JavaScript: ada.constructor found by prototype lookup
In real life: Replacing the shelf with an unlabeled one
In JavaScript: User.prototype = { greet() {} }

Where the analogy stops: The label is useful metadata, not the mechanism that creates objects. new uses the function you called, not a constructor label found on an existing instance.

Swap the prototype shelf
Step 0 of 8Ready
Your turn: follow the blue line

Switch the setting to restore or omit constructor, then step through which shelf each instance uses.

Running in
  1. script
Next: line 4
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
function User(name) {  this.name = name;}  return "Old shelf: " + this.name;};const ada = new User("Ada"); User.prototype = {  greet() {    return "New shelf: " + this.name;  },};const lin = new User("Lin"); console.log(ada.greet());console.log(lin.greet());console.log(ada.constructor === User);console.log(lin.constructor === User);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Choose whether the replacement shelf restores constructor

Changing the shelf starts a fresh replay. Watch Ada keep the old shelf.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Two details matter in real projects. First, replacing User.prototype does not move existing instances: Ada keeps pointing at the old object. Second, object literals do not automatically include constructor: User. If code relies on instance.constructor, restore it when you replace the whole prototype object.

Sharing methods instead of copying methods

INTERACTIVE

A method created inside a constructor is a new function object every time the constructor runs. That is sometimes useful when a method must close over private data, but it is wasteful for ordinary behavior. Putting the method on F.prototype creates one function object that all instances share.

How many function objects are created?
Two constructor stylesPop out in the code editor (opens in a new tab)JavaScript
function UserOwn(name) {  this.name = name;  this.greet = function () {    return "Hi, " + this.name;  };} function UserShared(name) {  this.name = name;}UserShared.prototype.greet = function () {  return "Hi, " + this.name;};
Real identity comparison4 instances
  • Inside constructorfalse

    4 distinct greet functions.

  • On prototypetrue

    1 shared greet function.

Try it yourself
Instances:

With 4 instances, the constructor-created method makes 4 distinct function objects. The prototype method makes 1.

Both designs work. The prototype design shares one method while each instance still has its own name.
Where things live in constructor-based code
ThingWhere it livesWhat lookup does
Own field like nameOn each instanceFound immediately on ada or lin.
Shared method like greetOn User.prototypeFound after the instance does not have its own greet.
constructorUsually on User.prototypeFound by lookup unless you replace the prototype and forget to restore it.
Parent methodOn User.prototype, above Admin.prototypeFound after checking the instance and Admin.prototype.

This is why older JavaScript code often assigns methods after the constructor declaration. The method still receives the instance as this when called as ada.greet().

Instance or prototype?
  • ada.name after new User("Ada")
  • maya.level after new Admin("Maya", 2)
  • User.prototype.greet
  • User.prototype.constructor
  • Admin.prototype.greet override
  • User.prototype.greet when maya calls the parent method
Try it yourself
0 of 6 correct

Sort each property or method by where it lives during lookup.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Inheriting between constructors

STEP THROUGH

Before class syntax, JavaScript code connected constructor functions directly. The classic pattern has three jobs:

  1. Inside the child constructor, call the parent constructor with User.call(this, name) so own fields are initialized on the same new object.
  2. Set Admin.prototype = Object.create(User.prototype) so admin instances can find user methods by lookup.
  3. Restore Admin.prototype.constructor = Admin, then add or override child methods.
Build Admin from User
Step 0 of 10Ready
Your turn: follow the blue line

Classic constructor inheritance is two jobs: call the parent constructor for own fields, and link the child prototype to the parent prototype for methods.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
  this.name = name;}User.prototype.greet = function () {  return "Hi, " + this.name;}; function Admin(name, level) {  User.call(this, name);  this.level = level;}Admin.prototype = Object.create(User.prototype);Admin.prototype.constructor = Admin;Admin.prototype.greet = function () {  return User.prototype.greet.call(this) + " (admin " + this.level + ")";}; const maya = new Admin("Maya", 2);console.log(maya.greet());console.log(maya instanceof Admin);console.log(maya instanceof User);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Overriding means the child prototype defines a property with the same name as the parent method. Lookup finds Admin.prototype.greet before User.prototype.greet. Calling User.prototype.greet.call(this) deliberately reuses the parent method with the same instance as this.

Classes set up the same prototype links

COMPARE

Classes come next module, so do not worry about mastering their syntax yet. The important bridge is that class methods also live on the class’s prototype, and extends links the child prototype to the parent prototype.

Class syntax, same linksPop out in the code editor (opens in a new tab)JavaScript
class User {  constructor(name) {    this.name = name;  }  greet() {    return "Hi, " + this.name;  }} class Admin extends User {  constructor(name, level) {    super(name);    this.level = level;  }  greet() {    return super.greet() + " (admin " + this.level + ")";  }} const maya = new Admin("Maya", 2);console.log(maya.greet());console.log(Object.getPrototypeOf(Admin.prototype) === User.prototype);

The class version prints the same greeting as the constructor version, and Object.getPrototypeOf(Admin.prototype) === User.prototype is still true. super(name) fills the role of User.call(this, name); super.greet() fills the role of User.prototype.greet.call(this).

Which callables have .prototype?Pop out in the code editor (opens in a new tab)JavaScript
console.log((function User() {}).prototype !== undefined);console.log((() => {}).prototype === undefined);const tools = { build() {} };console.log(tools.build.prototype === undefined);class Panel {}console.log(typeof Panel.prototype);

Ordinary functions and classes have a prototype property because they can be construction targets. Arrow functions cannot be used with new, so their prototype is undefined. Methods written with object or class method syntax also are not constructors.

Where you’ll use this

You will meet constructor prototypes whenever you read older libraries, inspect objects in DevTools, debug instanceof, or design objects that share behavior. Even modern class code is easier to debug when you know the prototype chain it creates.

Practical checklist
  • Put per-object data, such as name, on the instance.
  • Put reusable methods, such as greet, on the prototype.
  • When replacing a prototype object, restore constructor if code will read it.
  • For inheritance, initialize own fields and link prototypes; you need both.

Common misconceptions

  • “F.prototype is the same as an instance’s prototype.” It is the object future instances link to. The instance’s link points at it.
  • “Prototype methods are copied into every object.” They are shared by lookup. Identity checks prove it.
  • “constructor creates the object.” It is just a property, usually inherited from the prototype.
  • “Replacing F.prototype updates old objects.” Old objects keep the old linked object.
  • “Calling the parent constructor sets up method inheritance.” It initializes fields only; Object.create sets the method chain.
  • “Classes are a different inheritance system.” They are cleaner syntax over prototype links.

Practice exercises

5 EXERCISES
Exercise 1 · Warm-upPredict prototype relationships

Run the code mentally first. Then type the two logged values.

Starter codePop out in the code editor (opens in a new tab)JavaScript
function User(name) {
  this.name = name;
}
User.prototype.greet = function () {
  return "Hi " + this.name;
};
const ada = new User("Ada");
console.log(Object.getPrototypeOf(ada) === User.prototype);
console.log(ada.hasOwnProperty("greet"));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticeMove a method to the prototype

    Rewrite the constructor so all users share one greet function.

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    function User(name) {
      this.name = name;
      this.greet = function () {
        return "Hi, " + this.name;
      };
    }

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeFix a missing constructor label

      Repair the replacement prototype so instances find User through constructor.

      Starter codePop out in the code editor (opens in a new tab)JavaScript
      function User(name) {
        this.name = name;
      }
      User.prototype = {
        greet() {
          return "Hi, " + this.name;
        },
      };

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · ChallengeSet up constructor inheritance

        Complete the classic pattern so an admin is also an instance of User.

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        function User(name) {
          this.name = name;
        }
        User.prototype.greet = function () {
          return "Hi, " + this.name;
        };
        function Admin(name, level) {
          // initialize User's fields here
          this.level = level;
        }
        // link Admin.prototype here

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeConvert the constructor pattern to class syntax

          Write the equivalent class User and check the greeting.

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          function User(name) {
            this.name = name;
          }
          User.prototype.greet = function () {
            return "Hi, " + this.name;
          };

          Answer, then press Check. Spacing and letter case don’t matter.

            Check your understanding

            7 QUESTIONS
            F.prototype & constructors quiz · 7 questionsScore: first tries count
            1. Question 1 of 7What does new User() do with User.prototype?

              Choose an answer to see the explanation.

            2. Question 2 of 7What does the shared method comparison print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              function User(name) {
                this.name = name;
              }
              User.prototype.greet = function () {
                return "Hi " + this.name;
              };
              const ada = new User("Ada");
              const lin = new User("Lin");
              console.log(ada.greet === lin.greet);

              Choose an answer to see the explanation.

            3. Question 3 of 7After User.prototype = { greet() {} }, why can new User().constructor === Object be true?

              Choose an answer to see the explanation.

            4. Question 4 of 7Which line connects Admin instances to User.prototype methods?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              function Admin(name) {
                User.call(this, name);
              }
              Admin.prototype = Object.create(User.prototype);
              Admin.prototype.constructor = Admin;

              Choose an answer to see the explanation.

            5. Question 5 of 7What does this print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log((() => {}).prototype === undefined);

              Choose an answer to see the explanation.

            6. Question 6 of 7What is usually best for methods shared by many instances?

              Choose an answer to see the explanation.

            7. Question 7 of 7How does class Admin extends User relate to the constructor pattern?

              Choose an answer to see the explanation.

            Key takeaways

            • new F() links the new object to F.prototype.
            • Own fields belong on each instance; shared methods belong on the prototype.
            • constructor is normally inherited from the prototype and must be restored after replacing it.
            • Constructor inheritance needs both parent initialization and prototype linking.
            • Class syntax creates the same kind of prototype relationships with clearer syntax.

            Final definition: F.prototype is the shared object that new F() places in each instance’s prototype chain.

            Up next: Built-in prototypes.

            CompleteFrontend Clear concepts. Working examples.