3gwebtrain
3gwebtrain

Reputation: 15293

'Prototype' throws TypeError: info is undefined

I am trying to inherit a function by prototype, it seems all are correct (according to me), but I am getting error... any one help me to understand this issue?

function:

(function($){
    var Man = function(info){
        this.name = info.name;
        this.age = info.age;
        this.work = info.work;
    }

    Man.prototype.tell = function(){
        return 'I am Mr.' + this.name;
    }

    var Dog = function(info){
        Man.call(this,info);
    }

    Dog.prototype = new Man();

    var dog1 = new Dog({name:'Dobber',age:3,work:'roming'});
    console.log(dog1.tell());

})($);

error I am getting:

TypeError: info is undefined

Upvotes: 0

Views: 805

Answers (1)

Pointy
Pointy

Reputation: 413712

When you call new Man() setting up the Dog prototype, you're not passing an argument. The function expects one (that's what info is supposed to be).

I'm not sure how to correct it because the setup in that code doesn't make a lot of sense.

edit — maybe just make the Man constructor check the parameter:

function Man(info) {
  if (info !== undefined) {
    this.name = info.name;
    this.age = info.age;
    this.work = info.work;
  }
}

Upvotes: 6

Related Questions