Bob.setAge = setAge;


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/4?curriculum_id=506324b3a7dffd00020bf661#

So here is my code it works and I understand that it works and mostly how it works. I have made a function method called setAge and this method changes the .age of whatever it is called on to defined newAge. What I don't understand is bob.setAge = setAge line. What was the purpose of adding this new property to bob? What is it doing for me?


// 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;
// and down here we just use the method we already made
bob.setAge = setAge;
  
// change bob's age to 50 here
bob.setAge(50);


#2

Notice the this object? It has an age property, this.age; however, there is no context object associated with it (that we defined). There is one that we didn't define, window.

var setAge = function (newAge) {
  this.age = newAge;
};

setAge(24);
console.log(age);    // 24

That's weird, sort of. We didn't declare a variable, age so why didn't we get a reference error?

Because the variable does get defined by the function... this.age, with window as the context object.

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

What this line does is give bob the same method we gave window.

bob.setAge = setAge;

When we call the method on bob, the context is one we defined, rather than the global object.

If we enumerate the bob object, we will see that it has both an age property, and a setAge property.


#3

... Ok so I still don't understand so maybe I can try putting this into logic and you can correct me if I am wrong.

We define the function:

var setAge = function (newAge) {
this.age = newAge;
};

Which states that whatever object owns this function the new value of its.age is now whatever is defined as newAge when calling it. But this does not have a value of an object yet. So first we make an object.

var bob = new Object();
bob.age = 30;
bob.setAge = setAge;

This object has two properties age and setAge. .age has the value of thirty, and setAge has the value of the function. The value of the function would be that bobs age changes its value to now defined by calling the function on this. so technically.

bob.setAge = (bob.age = newAge)

which of course we define by calling on the function on this object. Had we not assigned bob the method then bob.age = 30 would have satisfied that logic but what we need to call is the function on the object that owns the method. When we assign the function to bob.setAge it recieves that this keyword now holds the value bob. It also recieves the definition for .age.

so after having declared that the bob object now owns the setAge method the computer now understands that this can be bob and knows the value it is overwriting.... at this point it is a method and can be called on any object owning a property of age.

bob.setAge(50);

thats not easy to instill as common logic.


#4

so bob.setAge is a property and it owns the function when we say = setAge


#6

OK if I cant explain it simply I don't understand it well enough so I am sorry for repeating myself but let me try this one more time.

bob.setAge = setAge asings bob to own the setAge function and creates an environment to permanently store the outcome value of given argument.

PLEASE tell me that's right.


#7

Not a correct representation. Consider the following:

setAge(25)

invokes the function on the window object, so that window.age is 25.

bob.setAge = setAge

copies a string to the property on bob object. This is what gets copied:

"function (newAge) {
     this.age = newAge;
}"

Notice that we do not invoke the function, only copy its string. Now we can invoke it on bob.

bob.setAge(25);

Since the function defines a local variable,this.age the variable need not exist previous to running the method. Remember above where we didn't define age?


#8

I won't try to explain it, right now, but add 'closures' to your advanced reading topic list. Objects with functions as properties are closures on the variables, which continue to exist even after the function is exited. Don't worry if you find this hard to understand. In time, with reading and practice it will make sense. For now, continue to progress through the track and keep notes so you can review and research each topic.


#9

the object property has the value of a string? So there really is no point in the value of the property. Was the purpose of it to just accurately state
bob.setAge
so that the object owns the function? I mean if so I guess that makes sense but if that's the case the value really serves no purpose does it?


#10

I'll research closures right now and thank you for helping me.


#11

Also if the method has rewritten the .age value then where does it store that information? Is it still stored in the bob.setAge?


#12

The object is the only execution context that the method knows.

As the age property value.

A value is basically any primitive object, such as a number or string, and any expression that yields a value, such as a boolean expression or a function expression. So a property can have any object as a value.

myObj = {
    num: 42,
    str: "Cosmos",
    arr: [],
    obj: {},
    adda: function (value) { this.arr.push(value); }
    addo: function (key, value) { this.obj[key] = value; }
};

#13

var setAge = function (newAge) {
this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
// and down here we just use the method we already made
bob.setAge = setAge;
setAge(50);
it's not working!
what's wrong with this?
setAge is function and i am passing the argument for that function.
bob.setAge is indirectly a function right? because it's assigned with function named setAge!
get me out of this confusiuon


#14

Now that setAge() is a property of bob, we must invoke it wtih its identifierName,bob.setAge...

    bob.setAge(50);
    console.log(bob.age);    // 50