SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TypeError: Object # has no method 'getName'

    Hello!

    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!

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Possibly because document.write is disallowed in JSFiddle

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code:
    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");
    console.log(smallZombie.getName());
    console.log(smallZombie.getType());
    console.log(smallZombie.eatPeople());

  4. #4
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I see! Thank you very much

  5. #5
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to Pullo

    Why I cannot delete this property from your program?

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

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by 8Observer8 View Post
    Why I cannot delete this property from your program?
    because it's not a property, it's a method defined on the Zombie prototype object.

    Code:
    var smallZombie = new Zombie("Ingo");
    console.log(smallZombie.getName());
    console.log(smallZombie.getType());
    console.log(smallZombie.eatPeople());
    
    => Ingo
    => Booger
    => Tastes like chicken
    Code:
    // Remove property
    delete smallZombie.name;
    console.log( smallZombie.getName() );
    
    => undefined
    Code:
    // Remove method
    delete Zombie.prototype.eatPeople;
    console.log( smallZombie.eatPeople() );
    
    => Uncaught TypeError: Object #<Monster> has no method 'eatPeople'

  7. #7
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understood! Thanks

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Pullo View Post
    because it's not a property, it's a method defined on the Zombie prototype object.
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •