1

Hi everybody first time posting here.

So I am trying to understand something, I will post the code here too, but if you want to check out the source, its this " https://www.codecademy.com/en/courses/spencer-sandbox/2/5?curriculum_id=506324b3a7dffd00020bf661 "

Which is an exercise from codeacademy....

So there is bob.setAge = setAge; , why didn't it make it so bob.setAge= newAge; ?? here is the code .

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(35);
3
  • setAge is a function ... later you can call it bob.setAge(30). Commented Dec 25, 2015 at 18:13
  • bob.setAge= setAge define the setAge method of the object to be the function setAge, after that the method could be call and a value passed as parameter. Commented Dec 25, 2015 at 18:15
  • 2
    Not sure why this was closed - the question was very clear. Commented Dec 25, 2015 at 18:21

1 Answer 1

1

I believe this exercise is trying to teach you the fundamentals of object oriented programming in JavaScript without the syntactic sugar.

More simply stated, they are making a person and that person can have a set of attributes that describe them, and a set of actions they can do.

You've created this function here called setAge

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

It'll set the age of an this to newAge. But the question here is what is this? The answer is that it depends, which is why you can reuse this function.

So when you "make bob"

// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;

What you're doing is saying that bob has an action he can do called setAge, and this action is equivalent to the function we defined earlier called setAge. Also in this context this is refers to bob.

So when you do something like bob.setAge(20) then bob.age, represented by this.age will be changed from 30 to 20.

Does that make sense?

Also in your link there is this quote

Great! Now we can take advantage of the fact that the method setAge is not limited to a single object bob

Whether it's bob or susan, you can always use the same setAge function you defined above if you assign it to the object.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes i understand now. thanks a lot.... trying to learn java script can be confusing sometimes but the fast answer was really appreciated thanks again <3
Yeah no problem. Glad I was able to get an answer in before they closed it. They can be pretty unfriendly to new programmers here :/
im new here, i don't even know why they closed it :o. like i just made my account 10 minutes ago and then added my question.
@Munkiuke the problem was that you wrote trying to understand how this works in javascript which isn't a very descriptive title. I edited it to make it more clear, but it seems like I was too late. Next time just try to have your title be more specific.
ohhhhhh alright i understand now. thanks, i will try to be clear in the title next time. what does this ( on hold ) mean ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.