Q: The intent for enhancing lessons is that
-
A
objects inherit prototype properties even in a dynamic state
-
B
objects inherit prototype properties only in a dynamic state
-
C
objects inherit prototype properties in the static state
-
D
object doesn
A
Answer:
A
Explanation:
In JavaScript, inheritance is implemented using prototypes, which are objects that serve as templates for creating other objects. When an object is created, it inherits properties and methods from its prototype, which can be another object or null. This is known as prototype-based inheritance.
One of the characteristics of prototype-based inheritance in JavaScript is that it is dynamic. This means that the prototype of an object can be changed at any time, and the changes will be reflected in all objects that inherit from it.
This is different from classical inheritance in languages like Java, where the inheritance hierarchy is fixed at the time the object is created and cannot be changed later.
In JavaScript, we can augment a class (which is implemented as a constructor function) by adding new methods to its prototype object.
For example:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
}
const person = new Person('Alice');
person.sayHello(); // Output: "Hello, my name is Alice"
Here, we define a Person constructor function that takes a name parameter and assigns it to the name property of the object being created.
We then add a sayHello method to the Person prototype object. When we create a new Person object using the new keyword, the object inherits the sayHello method from its prototype, so we can call it on the object.
Related Topic:
Share Above MCQ