Why does it not print to console? [4. Can I See Your References?]


#1


https://www.codecademy.com/en/courses/objects-ii/0/4?curriculum_id=506324b3a7dffd00020bf661


Why does the code not print james.job to the console?


var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        console.log("Hi, I work as a " + this.job)
    };
};

james.sayJob();

james.job = "super programmer";

james.sayJob();


#2
var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        // complete this method
        console.log("Hi, I work as a " + this.job); //I have added a semi colon here
    } //I have not put a semi colon here
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer"

// james' second job
james.sayJob();

#3

Why are you writing

this.job

?

You're method is inside the james object, so you can just write james.job.

Here's my code

> var james = {
>     job: "programmer",
>     married: false,
>     sayJob: function() {
>         // complete this method
>         console.log("Hi, I work as a " + james.job);
>     }
> };

> // james' first job
> james.sayJob();

> // change james' job to "super programmer" here
> james.job = "super programmer";

> // james' second job
> james.sayJob();

#4

Writing this.job makes the job variable applicable to all objects that inherit from that class/method. :slight_smile:
But if you write james.job you are making that variable private and can only be used by james.


#5

Is that the case any time one uses this?


#6

Yes, but it only works for functions or methods.