What's wrong in this?


#1


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


TypeError: bob.setAge is not a function


// 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(50);


#2

where did this line go:

// and down here we just use the method we already made
bob.setAge = setAge;

which was provided by the exercise? Without it, you never link bob object and setAge method


#3

yup got that later. Why do we need this linking? I am a C/C++ code and that's why couldn't understand this.


#4

well, i personally find this a bit weird. I would prefer to use:

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

var bob = new Person(30);
bob.setAge(50)
console.log(bob.age)

which makes more sense (in my opinion), which is a custom constructor which you will get in a few exercises. This allows methods to be added directly to an object, rather then first defining the method, and then adding it to object


#5

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