17 Won't Work For Me


#1

// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;

// make susan here, and first give her an age of 25
var susan = new Object();
susan.age = 25;
susan.setAge = setAge;
// here, update Susan's age to 35 using the method
susan.setAge = setAge(35);


#2

The following,

susan.setAge = setAge(35);

will set the susan.setAge property to undefined...

{ age: 25, setAge: undefined }

However, if we inspect the age property of window,

console.log(window.age); // 35

This,

susan.setAge(35);

will set the susan.age property to 35...

{ age: 35, setAge: [Function] }