JavaScriptOop And Prototypesintermediate

JavaScript OOP and Prototypes Interview Questions

How JavaScript really does objects — prototypes, classes, Object.create, private fields, getters and statics, mixins, in vs hasOwn and Object.freeze — in eight interview questions with code.

Published
TL;DR – Quick Answer

JavaScript OOP interviews test that you know classes are built on prototypes: how the prototype chain resolves properties, what new and Object.create do, how private fields, getters, setters and static members work, mixins, the in operator versus hasOwnProperty, and Object.freeze versus const. Interviewers often ask you to explain the same idea with a class and with plain prototypes.

On This Page

JavaScript has classes, but underneath they are objects linked to other objects through prototypes — and interviewers love to check whether you know that. This guide covers prototypal inheritance, ES6 classes, Object.create versus new, encapsulation, getters, setters and statics, mixins, property lookups and freezing objects.

Why interviewers ask about OOP and prototypes

Developers who arrive from Java or C# often treat JavaScript classes as identical to the ones they know, and then get surprised by this, by methods shared through the prototype, or by inheritance that can be changed at runtime. These questions check that you understand the model the language actually uses.

The topic also comes up in framework work: React class components, Angular services and Node.js libraries all rely on classes and prototypes behaving the way the specification says.

How to answer these questions

Explain each idea twice: once with class syntax and once with the prototype it creates. Saying "a class method lives on ClassName.prototype and every instance looks it up through the chain" proves you understand what the syntax hides.

Many of these answers depend on how this is set, so revisit the closures and functions guide if that rule is not automatic yet. Coming from Java? The Java vs JavaScript comparison explains the differences in the object model.

Q1. What is prototypal inheritance in JavaScript?

Objects inherit properties and methods from other objects through the prototype chain. Every object has an internal [[Prototype]] link, and property lookups walk up that chain until they find a match or reach null.

function Animal(name) { this.name = name; }
Animal.prototype.speak = function () {
  return `${this.name} makes a sound.`;
};
const dog = new Animal("Rex");
dog.speak(); // "Rex makes a sound."
// Object.getPrototypeOf(dog) === Animal.prototype → true

Q2. What are ES6 classes?

ES6 classes are cleaner syntax over prototypal inheritance — class, constructor, extends and super — but underneath, methods still live on the prototype.

class Animal {
  constructor(name) { this.name = name; }
  speak() { return `${this.name} makes a sound.`; }
}
class Dog extends Animal {
  speak() { return `${this.name} barks!`; }
}
const d = new Dog("Rex");
d.speak();           // "Rex barks!"
d instanceof Animal; // true

Interview note: Follow-up: "what is typeof a class?" It is "function" — a class is a special constructor function, which shows it is built on prototypes.

Q3. What is the difference between Object.create() and the new keyword?

new Fn() creates an object whose prototype is Fn.prototype and runs the constructor. Object.create(proto) creates a plain object with proto as its prototype and runs no constructor.

const proto = { greet() { return `Hi, ${this.name}`; } };

const obj = Object.create(proto);
obj.name = "Siva";
obj.greet(); // "Hi, Siva"

// new keyword
function Person(name) { this.name = name; }
const p = new Person("Siva");
p.greet; // undefined — there is no greet on Person.prototype

Q4. What is encapsulation and how is it achieved in JavaScript?

Encapsulation bundles data with the methods that use it and hides internal state. In JavaScript you get it with closures (private variables in a factory function) or class private fields (#field), which the engine enforces.

class BankAccount {
  #balance = 0;
  deposit(amount) { this.#balance += amount; }
  get balance() { return this.#balance; }
}
const acc = new BankAccount();
acc.deposit(500);
acc.balance;   // 500
acc.#balance;  // SyntaxError — truly private

Q5. What are getters, setters and static members in a class?

Getters and setters (get/set) run code when a property is read or assigned, so you can compute or validate values. static members belong to the class itself, not to its instances — you call them as ClassName.method().

class Temperature {
  static unit = "°C";
  #celsius = 0;

  get fahrenheit() { return this.#celsius * 9 / 5 + 32; }
  set celsius(v) {
    if (typeof v !== "number") throw new TypeError("Number expected");
    this.#celsius = v;
  }

  static fromFahrenheit(f) {
    const t = new Temperature();
    t.celsius = (f - 32) * 5 / 9;
    return t;
  }
}
const t = Temperature.fromFahrenheit(212);
t.fahrenheit;     // 212
Temperature.unit; // "°C" — on the class, not on t

Interview note: Follow-up: "can an instance call a static method?" Not directly — t.fromFahrenheit is undefined, because static methods live on the constructor.

Q6. What are mixins in JavaScript?

Mixins add reusable behaviour to a class without a deep inheritance chain — typically as functions that take a base class and return an extended one.

const Serializable = (Base) => class extends Base {
  serialize() { return JSON.stringify(this); }
};

const Validatable = (Base) => class extends Base {
  isValid() { return Object.keys(this).length > 0; }
};

class User extends Serializable(Validatable(class {})) {
  constructor(name) { super(); this.name = name; }
}
new User("Siva").serialize(); // '{"name":"Siva"}'
new User("Siva").isValid();   // true

They help avoid the fragile base-class problem of classical inheritance.

Q7. What is the difference between the in operator and hasOwnProperty?

in checks the object and its whole prototype chain. hasOwnProperty — or the newer Object.hasOwn — checks only the object's own properties.

const base = { greet() {} };
const obj = Object.create(base);
obj.name = "Ravi";

"name" in obj;                // true
"greet" in obj;               // true  — found on the prototype
Object.hasOwn(obj, "greet");  // false — not an own property
Object.hasOwn(obj, "name");   // true

A for...in loop also walks inherited enumerable properties, which is why older code filtered it with hasOwnProperty. Object.keys() returns own properties only.

Q8. What is the difference between Object.freeze() and const?

const stops the variable from being reassigned; Object.freeze() stops the object itself from changing — no adding, removing or editing properties. Freezing is shallow, so nested objects can still change.

const config = Object.freeze({ env: "prod", db: { host: "a" } });
config.env = "dev";    // ignored (TypeError in strict mode)
config.db.host = "b";  // works — freeze is shallow
config.db.host;        // "b"

Object.seal() sits in between: existing properties can be edited, but none can be added or removed.

Interview note: Follow-up: "how do you deep-freeze an object?" Call Object.freeze recursively on every nested object.

How to prepare

Model one small domain — a bank account or a shopping cart — three ways: a factory function with closures, a constructor function with a prototype, and an ES6 class with private fields. Explain which you would choose and why.

Then continue to advanced JavaScript, where Symbols, Proxies and iterators extend the same object model.

Frequently Asked Questions

Is JavaScript an object-oriented language?
Yes, but it is prototype-based rather than class-based. Objects inherit directly from other objects through the prototype chain. ES6 classes are a cleaner syntax for that same mechanism, not a separate class system like Java's.
What is the prototype chain?
Every object has a hidden link to another object, its prototype. When you read a property that the object does not have, JavaScript looks at the prototype, then the prototype's prototype, and so on until it finds the property or reaches null.
What is the difference between __proto__ and prototype?
prototype is a property of constructor functions and classes: it becomes the prototype of instances created with new. __proto__ is the old accessor for an object's actual prototype. Prefer Object.getPrototypeOf and Object.setPrototypeOf in modern code.
How do you make a property private in JavaScript?
Use class private fields with a # prefix, which the engine enforces, or keep the value in a closure inside a factory function. The older underscore convention (_balance) is only a naming hint and does not prevent access.
Does JavaScript support multiple inheritance?
Not directly — each object has one prototype. You get similar results with mixins, functions that add methods to a class, or with composition, where an object holds other objects and delegates to them.
Should I use classes or factory functions?
Both are valid. Classes are familiar, work well with tools and frameworks, and share methods through the prototype. Factory functions with closures give true privacy without # syntax and avoid this entirely. Teams usually pick one style and stay consistent.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 28 November 2026 LinkedIn
Chat with us