JavaScript prototypes and inheritance MCQs

1. What is a Prototype?

In JavaScript, every object has an internal link to another object called its prototype. This prototype object can also have a prototype, and so on — this forms a "prototype chain".

  • The prototype is accessible via __proto__ (or Object.getPrototypeOf(obj)).
  • Functions in JavaScript have a special property called prototype (used when the function is used as a constructor).
  • Objects inherit methods and properties from their prototype.

Example:

let obj = {};
console.log(obj.__proto__ === Object.prototype); // true
console.log(Object.prototype.__proto__); // null

2. Prototype Chain

When accessing a property or method on an object, JavaScript will first check the object itself. If it doesn't find it, it will go up the prototype chain until it finds it or reaches null.

let obj = {
  name: "Alice"
};

console.log(obj.toString()); 
// Looks for toString in obj → not found
// Looks in Object.prototype → found

3. Creating and Extending Prototypes

When creating a constructor function, we can define shared methods using its prototype to avoid duplication across instances:

function Person(name) {
  this.name = name;
}

// Adding method to prototype
Person.prototype.sayHello = function() {
  console.log("Hello, my name is " + this.name);
};

let person1 = new Person("John");
let person2 = new Person("Jane");

person1.sayHello(); // Hello, my name is John
person2.sayHello(); // Hello, my name is Jane

Both instances share the same sayHello method from the prototype.

4. Inheritance Using Prototypes

JavaScript supports inheritance by setting up the prototype of one constructor to be an object created from another constructor's prototype.

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  console.log(this.name + " makes a noise.");
};

function Dog(name) {
  Animal.call(this, name); // Inherit properties
}

// Inherit methods
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

// Override speak method
Dog.prototype.speak = function() {
  console.log(this.name + " barks.");
};

let dog = new Dog("Rex");
dog.speak(); // Rex barks.

5. ES6 Classes (Syntactic Sugar)

ES6 introduced the class syntax which is easier and cleaner but works the same under the hood with prototypes.

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(this.name + " makes a noise.");
  }
}

class Dog extends Animal {
  speak() {
    console.log(this.name + " barks.");
  }
}

const dog = new Dog("Buddy");
dog.speak(); // Buddy barks.

6. Key Concepts Recap

  • Every object in JavaScript has a prototype.
  • Prototype chain is how inheritance is handled in JS.
  • Constructor functions can share methods using their prototype.
  • ES6 classes provide a neater syntax for prototype-based inheritance.

7. Useful Prototype Methods

  • Object.getPrototypeOf(obj) – Gets the prototype of an object.
  • Object.setPrototypeOf(obj, proto) – Sets the prototype of an object.
  • Object.create(proto) – Creates a new object with the given prototype.

Asynchronous JavaScript Quiz

1. What is the main feature of prototypal inheritance in JavaScript?

2. What does the Object.create() method do in JavaScript?

3. What is the purpose of the Object.assign() method?

4. How do JavaScript objects inherit properties from other objects?

5. What is the purpose of the prototype property in JavaScript?

6. How does prototypal inheritance differ from classical inheritance?

7. What does the constructor property of an object reference?

8. If you create an object using Object.create(proto), what does it inherit?

9. What is the result of using Object.assign() on two objects?

10. How can you access the prototype of an object in JavaScript?

11. What does the __proto__ property represent in JavaScript?

12. What happens to properties when an object is created with Object.create()?

13. How can you set an object's prototype in JavaScript?

14. What can be added to a prototype in JavaScript?

15. What is a key advantage of prototypal inheritance?

16. Can functions be added to the prototype of an object in JavaScript?

17. What does the 'instanceof' operator do in JavaScript?

18. How can an object created from a constructor function inherit properties?

19. What happens if a property is not found in an object during property access?

20. How does Object.create() facilitate inheritance in JavaScript?

Post a Comment

Your comment will be visible after approval.
© TechTestLog. All rights reserved. Premium By Raushan Design
//My script