function Animal() { this.alive = true; } Animal.prototype.breathe = function() { return 'Breathing'; }; function Dog(name) { this.name = name; } Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.bark = function() { return 'Woof!'; }; const myDog = new Dog('Buddy'); console.log(myDog.bark()); console.log(myDog.breathe()); console.log(myDog.alive);
The Dog prototype is linked to Animal.prototype, so myDog can access methods like breathe() defined on Animal.prototype. The property alive is set in the constructor Animal, but since Dog does not call Animal constructor, alive is undefined on myDog. Therefore, myDog.alive is undefined. So option D matches this.
The correct output is:
"Woof!" "Breathing" undefined
Therefore, option D is correct.
const obj = {}; console.log(obj.toString()); obj.toString = 5; console.log(obj.toString());
Initially, obj.toString() calls the inherited toString method from Object.prototype, printing '[object Object]'. After assigning obj.toString = 5;, obj.toString is no longer a function but a number. Calling obj.toString() then causes a TypeError because 5 is not callable.
Prototypes allow objects to share properties and methods without duplicating them in memory. This supports inheritance and code reuse. The other options describe unrelated features.
function Person(name) { this.name = name; } Person.prototype.greet = function() { return `Hello, ${this.name}`; }; const alice = new Person('Alice'); console.log(alice.greet()); Person.prototype.greet = function() { return `Hi, ${this.name}`; }; console.log(alice.greet());
The first call to alice.greet() uses the original method, printing "Hello, Alice". After modifying Person.prototype.greet, the second call uses the new method, printing "Hi, Alice".
const parent = { value: 42 }; const child = Object.create(parent); child.value = 100; console.log(child.value); delete child.value; console.log(child.value); delete child.value; console.log(child.value);
Initially, child.value is 100 (own property). After deleting it, child no longer has value, so JavaScript looks up the prototype chain and finds value in parent as 42. Deleting again does nothing since child has no own value.