Intro to Objects II: Can I see your references?


#1

Here is my code which was deemed correct:

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

james.sayJob();
james.job = "super programmer"; 
james.sayJob(); "I work as a" + " " + james.job;

Three questions:

(1) Originally I wanted to say "this.job = job" but as the "job" property is tied to the james object, it makes sense why I would need to have it equal "james.job" first. Is this reasoning correct?
(2) Why wouldn't I put anything into the parenthesis when calling the function at the end? Indeed, if I put in "james.job" there, it says "way to go" but it doesn't print out anything.
(3) Why do I need to type out the entire sentence "Hi, I work as . . ." twice, once in the function, and again at the end?


#2

1) Your function doesn't need the "this.job = james.job" line. That's unnecessary since james.job is already defined in the object.

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

}

2) You don't need to put anything in james.sayJob() since the function does not take in any parameters. You run the function by calling it, and it does whatever is in the method, which happens to be a print statement.

3) You don't need to type it twice. At the end, merely having james.sayJob() is enough to print the statement to the console. since the print command is contained within the function sayJob()