Bug in Objects II with "this" keyword


#1

Here is an interesting bug: for some reason, when I am writing a method, and I type the first parentheses after function, the "this" keyword changes color from red to purple. The code works fine and does log "Hello" to the console, but Codecademy says there is an error and won't let me proceed. Are other people getting this? Is this actually an error on my part that I'm not picking up on?


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

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


#2

Here is the code that worked for me.
The only difference I can find is I have a semi colon after the closing brace in this function, but you do not ..

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

The code in full ..

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

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

#3

I copied your code in, and it worked! Thank you. I have never had trouble with this before, but perhaps it is an obscure syntax rule.


#4

The mistake was trivial. It wanted you to print Hello! and you were missing the exclamation mark.


#5

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