-1

I'm a newbie in javascript and I'm wondering why bob.age is still 30 while age is 50 when I called it. I already set age to 50 in setAge that assigned 50 to this.age, which I know is a reference to bob.age, so age and bob.age should have the same value.

enter image description here

// 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

// change bob's age to 50 here
bob.setAge = setAge(50);
1
  • Please include the code in the question itself, not just an image, so it's easier for people to copy in their answers Commented Jul 11, 2015 at 4:30

3 Answers 3

6

There're several errors in your code/understanding, I'm going to start with why that is specifically wrong, and explain the concepts around it

var bob = new Object() is making bob a regular object.

your setAge function takes in a parameter. It sets this.age to be that value. However setAge is placed on the global scope. Therefore this -> window. Which is why when your typed in age in the global scope you got 50.

Jaromanda is correct, you need to place the function setAge onto the object bob.

If you're playing around with this you may be interested in the pseudoclassical instantiation style:

var Person = function(name, age) {
    this.name = name;
    this.age = age;
}

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

var bob = new Person ('Bob', 30);
bob.setAge(50);
bob.age; 
// 50
Sign up to request clarification or add additional context in comments.

5 Comments

The -> is likely misleading to newbies. I recommend changing it to a standard // comment
this makes me more confused
@user3783598: at what point did I lose you? Did the first half make sense? Through to the point that I said Jaromanda was right? Pseudoclassical is another thing, which you probably shouldn't worry about now, but I'd be happy to clarify/explain further.
I just don't get the prototype thing in the sample you've made but the rest is fine. Anyways I already understood it. Thanks
Person.prototype is an object you can attach functions to. Any Person will have access to all the functions on prototype. So even though bob technically only has name and age properties, he can access the functions attached to the prototype because he is a Person
3

bob.setAge = setAge(50);

all that is doing is setting the setAge property of bob to the result of calling setAge(50) - as setAge retunrs undefined, you've effectively set bob.SetAge = undefined. Perhaps what you intended to do was

bob.setAge = setAge; 
bob.setAge(50);

untested, but I think it's right

2 Comments

Wait why do you need to assign setAge to bob.setAge?
you mean the other way around, assign bob.setAge to setAge - see ChadF's more complete answer for an explanation
1

The keyword this acts as a placeholder, and will refer to whichever object called that method when the method is actually used.

Look at the method setAge to see how this works. By using the keyword this, setAge will change the age property of any object that calls it.

Then when we say bob.setAge = setAge; it means whenever we type bob.setAge( ) this.age in the setAge method will refer to bob.age.

1 Comment

I think you're just confused with bob.setAge which is a property of bob and setAge which is a function. So when you assign bob.setAge to setAge it means bob.setAge is defined by setAge and can access the setAgefunction by passing a value to parameter. bob.setAge(20);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.