What's wrong in this?



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


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


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


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);

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


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