-1

I have built a Constructor and added 3 functions to its prototype. When I try to call a function from the prototype i get this error

TypeError: undefined is not a function (evaluating 'fireOne.addLogs(8)')

Please can someone tell me what I am doing wrong. I cannot figure it out.

Thanks in advance

 function SignalFire(ID, startingLogs){
   this.fireID = ID;
   this.logsLeft = startingLogs;
 }


 var fireOne = new SignalFire(1, 20);
 var fireTwo = new SignalFire(2, 18);
 var fireThree = new SignalFire(3, 24);




 SignalFire.prototype = {

   addLogs: function(numLogs){
     this.logsLeft += numLogs;
   },


   lightFire: function(){
     alert("Whoooooosh!");
   },

   smokeSignal: function(message) {
     if (this.logsLeft < this.message.length / 10){
       alert("Not enough fuel to send " +
       "the current message!");
     }

     else {
       this.lightFire();
       var x = this.message.length;
       for(var i = 0; i < x; i++){
         alert("(((" + this.message[i] + ")))");
         if (i % 10 === 0 && i !== 0){
           this.logsLeft--;
         }
       }
     }
   }

 };

 fireOne.addLogs(8);
3
  • 6
    This would even work if you added them one by one (SignalFire.prototype.addLogs = function ...). But since you're outright replacing the entire prototype object, the reference is broken. Commented Dec 1, 2016 at 14:02
  • Just create your objects right before your fireOne.addLogs(8) Commented Dec 1, 2016 at 14:03
  • It is the order of your code. You create before you add the prototype. put the var fireXXX = new SignalFire() after the prototype object... Commented Dec 1, 2016 at 14:18

1 Answer 1

1

Problem is order of your code. You add things after it is created. If you add the prototype before you create the fires it will work.

function SignalFire(ID, startingLogs){
   this.fireID = ID;
   this.logsLeft = startingLogs;
 }

 SignalFire.prototype = {
    addLogs: function(numLogs){
     this.logsLeft += numLogs;
     console.log(this.logsLeft);
   }
 };

 var fireOne = new SignalFire(1, 20);
 fireOne.addLogs(8);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.