I dont completely understand this piece of code can someone please explain in some sort of detail


// 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 = {
age: 25,
setAge: setAge

Thank you if you helped


You simply just need to copy bob's code down and replace the bob with susan


The concept expressed here is that of pointer reference. When we assign a reference to a function to variable, that variable becomes a pointer to the function.


function foo() {
    console.log("I am foo!")

var bar = foo;

bar(); // I am foo!

Notice that we do not invoke foo() when referring it to bar. We have only copied the function's toString() value, not the outcome of the function.

Now a function written in global window context has window as this, so is not much of value in the global scope. When we refer (assign) the function to an object property, the context shifts to that object.

var bob = {};
bob.setAge = setAge;
console.log(bob.age); // 25

The same will apply to susan

var susan = {
    setAge: setAge
console.log(susan.age); // 30

Starting to make sense?