2.Fun with functions: why it doesn't work?


#1
function Person(job, married) {
    this.job = job;
    this.married = married;
    this.speak = console.log("Hello!");
}

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

It prints two times "Hello!" at the console and throws an error that says:

"TypeError: user.speak is not a function"

Why??? And why does it print two times? I think the code is right...


#2

Oh! I found the answer by myself. The right code is:

function Person(job, married) {
    this.job = job;
    this.married = married;
    this.speak = function() {
        console.log("Hello!");
    }
}

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

#3

that is the best, finding the answer yourself