Using parameter vs 'this.XXXXX' (4. Can I see your references)


#1

Hi guys, in the below code, the hint in the lesson suggests that we shouldn't use the parameter for the function but should instead use 'this.job'. What's the reason for this?

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

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

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

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

#2

Bump? :slightly_smiling:


#3

Are you referring to:

You should use this.job inside your method. It should not be a parameter, but is used as a part of the console.log statement.

I do not know the exact decision for writing this Hint (I am not the course writer :wink:), but from what I can guess, some users who are usually confused with JS might be writing this:

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

So that their confusion can be removed. And since you wrote the correct code, I don't think you have any need to worry :wink:

Hope it helps! :smiley:


This.name vs name