Where my code went wrong?


#1

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
this.job: 'super programmer' ;

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

Do I can use james and this arbitrary in place of each other or there is some specification regarding it


#2

There is a specification which relates to object context.

james.sayJob();

In the above, sayJob() is a method of the james object literal. All methods of this object have a common variable that specifies their execution context... this, the internal pointer to the reference object, james.

The operative word here is internal. In outer scope, where james exists, this is not accessible. To access the job property directly we have to go through public channels and use the formal, or property specific identifier,

james.job

Make sense?


#3

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