Trying to understand, there's no error, only need an explanation


#1

What exactly is happening at this line where it says: "bob.setAge = setAge;"
The default comment says we're using our already created method.
Things that are throwing me off :

  • setAge doesn't have a parenthesis.
  • setAge has no default value, with this said is the program saying bob.setAge = undefined?

It'd be great if you could break it down to me. Thanks in advanced.


// 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;
//here we use the method we already made
bob.setAge = setAge;
bob.setAge(50);
console.log(bob.age);

var bobbi = new Object();
bobbi.age=39;
bobbi.setAge = setAge;
bobbi.setAge(20);
console.log(bobbi.age);


#2

setAge refers to the function object, an executable string.

setAge() invokes the function.

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

This function is actually a newly defined method of the window object. Try it...

window.setAge(25);
console.log(age);    // 25

To give it a new execution context, we simply assign the method to a new object.

var bob = {};
bob.setAge = setAge;

We have given a copy of the executable string to this object. It is now an ownProperty.

console.log(bob.hasOwnProperty("setAge"));    // true

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

To view the executable string we render it using its .toString() method...

console.log(setAge.toString());
function (newAge) {
    this.age = newAge;
}

And we can see that bob has the same method,

console.log(bob.setAge.toString());
function (newAge) {
    this.age = newAge;
}

#3

Thanks, I appreciate the explanation :+1:


#4

You're welcome. I did leave something out, though.

If we remove the window method, it will have no bearing on the bob method.

 > setAge = null
=> null
 > console.log(setAge.toString());
TypeError: setAge is null
 > console.log(bob.setAge.toString());
function (newAge) {
    this.age = newAge;
}

We can still clone the executable string to another object.

var mary = {};
mary.setAge = bob.setAge;
console.log(mary.hasOwnProperty("setAge"))    // true

#5

I was messing around a bit with the code.
What mtf said I wasn't able to fully comprehend until now.
I tested some code and looked at what was output to the console.

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

console.log(bob.setAge);

Up until this point bob.setAge doesn't exist inside the object bob
So when we console.log(bob.setAge); It's output will be Undefined since the method wasn't declared in bob object yet.

bob.setAge = setAge;

This statement: bob.setAge = setAge basically sets the function we created above to the bob object, so later on in the program we can access that method, after this bob will have the properties age and setAge().

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

These last two lines simply output 50 to console since our method is properly working.


#6

Took some refreshed eyes to to finally understand this :slight_smile:


#7

Think of an object as one's self (a loose analogy). We have character traits. They are not contained in our body, but are part of our being. This is what object properties are more closely parallel with. Traits. They are not in the object as much as associated with the object. We have our own character, and an object instance has its own properties.

Some of us know how to fix engines. Most of us don't. Those who do have own properties that include the methods (skills) to perform mechanics. Think of methods this way associated with object instances and they begin to make sense.

If bob can fix an engine, and mary wishes to fix her own engine, she can learn from bob and clone his skills. An apt apprentice, so to speak. The apprentice does not need to know what bob knows, only have a copy of the procedure manuals that bob uses to perform his work. We saw earlier how easy it was for mary to clone the method from bob.

It's a loose analogy, I know. We get a feel for the mechanics, though, one thinks.


#8

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