Prototypal Inheritance
Prototypes / Inheritance: Prototypal Inheritance
What does a prototype refer to in JavaScript?
View Answer:
Overview: Historical example using proto, which is now deprecated (no longer supported)
let animal = {
eats: true,
};
let rabbit = {
jumps: true,
};
rabbit.__proto__ = animal; // (*) __proto__ deprecated
// we can find both properties in rabbit now:
console.log(rabbit.eats); // true (**)
console.log(rabbit.jumps); // true
You should be familiar with the __proto__
because you may see it in older code.
What exactly is a prototype in JavaScript when it comes to objects?
View Answer:
// Prototypal Inheritance
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User('Jack');
console.log(user.name); // Jack
console.log(user.isAdmin); // false
console.log(user instanceof User); // true
In JavaScript prototypal inheritance, what is the value of the “this” keyword?
View Answer:
// animal has methods
let animal = {
walk() {
if (!this.isSleeping) {
console.log(`I am walking`);
}
console.log("I'm asleep!");
},
sleep() {
this.isSleeping = true;
},
};
animal.walk(); // returns 'I am walking'
let rabbit = {
name: 'White Rabbit',
__proto__: animal,
};
// modifies rabbit.isSleeping
rabbit.sleep();
console.log(rabbit.isSleeping); // true
console.log(animal.isSleeping); // undefined (no such property)
Prototypes do not affect "this", regardless of the method the location in an object or prototype. This structure is always the object before the dot in a method call.
Can you explain the difference between Object.create() and the new keyword in relation to prototypal inheritance?
View Answer:
// Using Object.create()
let proto = { name: "Proto" };
let obj = Object.create(proto);
console.log(obj.name); // "Proto"
// Using new keyword
function Constructor() { this.name = "Constructor"; }
let newObj = new Constructor();
console.log(newObj.name); // "Constructor"
How does JavaScript implement inheritance compared to classical inheritance in other languages?
View Answer:
What is the difference between a prototype and a constructor in JavaScript?
View Answer:
// Constructor
function Car(make, model) {
this.make = make;
this.model = model;
}
// Create a new object using the Car constructor
let car1 = new Car('Toyota', 'Corolla');
console.log(car1.make); // Output: Toyota
console.log(car1.model); // Output: Corolla
// Prototype
Car.prototype.getDetails = function() {
return this.make + ' ' + this.model;
}
// Use the prototype method
console.log(car1.getDetails()); // Output: Toyota Corolla
In the above example, Car
is a constructor function. We use this constructor to create car1
. We then add a method getDetails
to Car.prototype
. Now every object created with new Car()
will have access to getDetails
.
What is the prototype chain, and how does it work in JavaScript?
View Answer:
How can you create a new object that inherits from another object in JavaScript?
View Answer:
let decoration = {
color: 'red',
};
let rose = Object.create(decoration);
console.log(rose.color) // rose color: red
Can you explain the role of the "this" keyword in prototypal inheritance?
View Answer:
How does the for-in loop handle inherited properties?
View Answer:
let decoration = {
color: 'red',
};
let circle = Object.create(decoration);
circle.radius = 10;
for (const prop in circle) {
console.log(prop);
}
// Returns radius, color
let animal = {
eats: true,
};
let rabbit = {
jumps: true,
__proto__: animal,
};
for (let prop in rabbit) {
let isOwn = rabbit.hasOwnProperty(prop);
if (isOwn) {
console.log(`Our: ${prop}`); // Our: jumps
} else {
console.log(`Inherited: ${prop}`); // Inherited: eats
}
}
The proto notation is not recommended in JavaScript because it is deprecated. The proto notation is a way of creating objects that inherit from other objects. However, the proto notation is not as efficient as other methods of creating objects, and it can be difficult to use. Therefore, it is recommended to use other methods of creating objects, such as the constructor function.
What are some advantages and disadvantages of using prototypal inheritance in JavaScript?
View Answer:
How do you prevent an object from inheriting properties from its prototype?
View Answer:
let obj = Object.create(null);
// Try to use a method from Object.prototype
console.log(obj.toString); // Output: undefined
In the above example, obj
doesn't have access to toString
method from Object.prototype
as it was created with no prototype.