Skip to main content

Function Prototype

Prototypes / Inheritance: Function Prototype

What is the function.prototype property in JavaScript?

View Answer:
Interview Response: In simple terms, the function.prototype is a regular property in a function. Every Object in JavaScript contains the prototype meaning a regular property with the prototype name.

Code Example:

let animal = {
eats: true,

function Rabbit(name) { = name;

Rabbit.prototype = animal; // references animal

let rabbit = new Rabbit('White Rabbit'); // rabbit.__proto__ == animal

alert(rabbit.eats); // true

Can you explain how the function.prototype property works in JavaScript?

View Answer:
Interview Response: Every function has the "prototype” property even if we do not supply it. The prototype object is a special type of enumerable object to which additional properties can be attached to and shared across all the instances of its constructor function. A function prototype property only gets used when a new Function gets called, and it assigns the prototype of the new object.

Technical Response: Even if we don't offer it, every function has the "prototype" attribute. When a new Function gets invoked, the function prototype property assigns the [[Prototype]] to the new object. After the function prototype property changes (func.prototype = {another object}), new objects generated by the new Function gets another object as [[Prototype]], while existing objects retains the previous one. A default prototype is an object with the sole constructor pointing back to the function itself.

Code Example:

function Rabbit() {}
// by default:
// Rabbit.prototype = { constructor: Rabbit }

let rabbit = new Rabbit(); // inherits from {constructor: Rabbit}

alert(rabbit.constructor == Rabbit); // true (from prototype)

What happens when you replace the default function prototype in JavaScript?

View Answer:
Interview Response: When you override the default prototype in an object, we lose access to the function constructor property of the prototype.

Code Example:

function Rabbit() {}
Rabbit.prototype = {
jumps: true,

let rabbit = new Rabbit();
alert(rabbit.constructor === Rabbit); // false