Search⌘ K
AI Features

Solution Review: Prototype Property

Explore how the prototype property affects method access in JavaScript objects. Understand why replacing a prototype causes errors and how modifying the prototype properly allows methods like talk to work on existing instances.

We'll cover the following...

In the previous lesson, you were given the following code to modify.

Javascript (babel-node)
function func1(name){
this.name = name;
}
var obj1 = new func1("Bran");
func1.prototype = {
talk: function(){
console.log ("Welcome " + this.name);
}
}
function display(){
var obj2 = new func1("Jon");
obj2.talk() // works
obj1.talk() // doesn't work
}
display()

Solution #

Javascript (babel-node)
function func1(name){
this.name = name;
}
var obj1 = new func1("Bran");
func1.prototype.talk = function(){
console.log ("Welcome " + this.name);
}
function display(){
var obj2 = new func1("Jon");
obj2.talk()
obj1.talk()
}
display()

Explanation #

This challenge tests your ...