Help stuck on 18


#1


keeps saying
Oops, try again. Make sure you added a speak method to the Person constructor
please help


function Person(job, married) {
    this.job = job;
    this.married = married;
    // add a "speak" method to Person!
    Person.speak = function(){
        console.log("Hello!")
        }
}

var user = new Person("Codecademy Student",false);
Person.speak();


#2

you can fix it with

this.speak = function(){
console.log("Hello");
};

inside your function function


#3

Thank you @micropro48948


#4

var user = new Person("Codecademy Student",false);
Person.speak();

Person is an Object constructor.

var user = new Person("Codecademy Student",false);

Variable user takes object Person with 2 arguments.
Lets create other users:

var user1 = new Person("Student 1",false);
var user2 = new Person("Student 2",false);
var user3 = new Person("Student 3",false);

And call method speak() 3 times because we created 3 users:

Person.speak();
Person.speak();
Person.speak();

Now, how do you think what you see in console? Answer:

Uncaught TypeError: Person.speak is not a function(…)

Look to the speak() method:

this.speak = function() {
   console.log("Hello!")
}

The value of this will become the new object when the constructor is used to create an object.
When we created user1 in this we set object properties for user1:

user1.job = 'Student 1'; // instead of this we have user1
user1.married = false;  // instead of this we have user1
user1.speak(); // 'Hello!'

When you call speak() method user1 says:

"Hello!"

Because instead of this we have user1 and in this case we call method only for user1.
The same are with user2 and user3. Instead of this you have user2 and user3.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.