TypeError: Object # has no method 'getName'


I’m new in JavaScript. Why I receive this message? “TypeError: Object # has no method ‘getName’”

Code: http://jsfiddle.net/8Observer8/6mDrx/

Thank you for advance!


Possibly because document.write is disallowed in JSFiddle

On closer inspection, the “no method” error you were seeing was due to the fact that you were setting the Zombie’s prototype to Monster, after adding the getName method to it.
This should do what you want:

function Monster(type){
  this.type = type;

Monster.prototype.getType = function() {
  return this.type;

function Zombie(name) {
  this.name = name;

Zombie.prototype = new Monster("Booger");

Zombie.prototype.eatPeople = function() {
  return "Tastes like chicken";

Zombie.prototype.getName = function() {
  return this.name;

var smallZombie = new Zombie("Devid");

Yes, I see! Thank you very much :slight_smile:

to Pullo

Why I cannot delete this property from your program?

             delete smallZombie.eatPeople;
             console.log( smallZombie.eatPeople() );


because it’s not a property, it’s a method defined on the Zombie prototype object.

var smallZombie = new Zombie("Ingo");

=> Ingo
=> Booger
=> Tastes like chicken
// Remove property
delete smallZombie.name;
console.log( smallZombie.getName() );

=> undefined
// Remove method
delete Zombie.prototype.eatPeople;
console.log( smallZombie.eatPeople() );

=> Uncaught TypeError: Object #<Monster> has no method 'eatPeople'

I understood! Thanks :slight_smile:

I wasn’t happy with this explination, so I asked @fretburner ; who pointed out that a method is just a function assigned to a property.

In his words, a better way to explain it would be that the delete operator only acts on properties of the specified object, it doesn’t traverse the prototype chain the way accessing a property does.
As the eatPeople function is a property/method of the Zombie prototype object, it has to be deleted from there.

Hope that helps some.