Home > database >  Problem with calling a method using a prototype
Problem with calling a method using a prototype

Time:02-08

Hi I have this code written and I want the output to be "bill jones is a snowboarder of beginner skill on the slopes.". I am getting "function () { return this.firstName " " this.lastName; } is a snowboarder of beginner skill on the slopes." I think the problem is the way I am calling the fullName method but I am stuck.

https://codepen.io/stefan927/pen/yLPJdQG?editors=1111

(function() {
  const fullName = document.getElementById('fullName');
  const type = document.getElementById('type');
  const ability = document.getElementById('ability');

  function Person(firstname, lastname, type, ability) {
    this.firstName = firstname;
    this.lastName = lastname;
    this.type = type;
    this.ability = ability;
  }
  Person.prototype.fullName = function() {
    return this.firstName   " "   this.lastName;
  }

  var skier = new Person('Bill', 'Jones', 'snowboarder', 'beginner');


  fullName.textContent = skier.fullName;
  type.textContent = skier.type;
  ability.textContent = skier.ability;

}())

CodePudding user response:

Like Pointy pointed ( no pun intended ) out in his comment, you are not calling the fullName function which is invoked with a pair of (). So, in your code fullName.textContent = skier.fullName should be fullName.textContent = skier.fullName(). That should resolve your issue.

CodePudding user response:

You have added fullName to the Persons prototype, and it's a function.
So you need to call/invoke this function.

fullName.textContent = skier.fullName(); //added () to fullName
  •  Tags:  
  • Related