This keyword


#1

// 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 =50;

// change bob's age to 50 here

My output is cmng bt its saying ,,make sure to change bob's age...whts my err


#2

Because function setAge(newAge) doesn't belong to object bob and you need add defined function to it.

var bob = new Object(); // here you created new empty object
bob.age = 30; // here you set property 'age = 30'. Now 'bob' looks like:
// var bob = {
//   age = 30 // 'bob' has only one property
// }

bob.setAge = 50; // here you want to set new value for property 'age' 
//using unknow method 'setAge'

//'bob' doesn't have this method to do it. That's why you have an error.

// Here the trick (from instruction): down here we just use 
//the method we already made
bob.setAge = setAge;

// Now 'bob' knows how he can change his property 'age'. 
//After that you can use function as a "bob's" method setAge(newAge) :
bob.setAge = 50;

I hope you understood where was your problem.


#3

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