0

can someone make me understand why i get undefined in my output in the last line?

var protoRabbit = {     
    speak: function(line){         
        print_line(this.type+' says, '+this.line);         
        print_line(this.type+' says, '+line);     
    },
    kill : function (){
        console.log('killerRabbit is not killing');
    }
};
var killerRabbit = Object.create(protoRabbit);
killerRabbit.type='killer';
killerRabbit.line='killer here';
killerRabbit.speak('killer rabbit here');
print_line(killerRabbit.kill());

op as on console:

killer says, killer here
killer says, killer rabbit here
killerRabbit is not killing
undefined

why is undefined there when i added a property(kill function to the initial object) ?

P.S. print_line is a custom function i made which takes an argument and prints output to console...

coolstoner
  • 719
  • 2
  • 9
  • 20
  • 2
    The `kill` function doesn't return anything, and the default returned value for any function is `undefined` – adeneo Oct 29 '16 at 17:20
  • changed `console.log('killerRabbit is not killing');` to `return 'killerRabbit is not killing';` and undefined is not appearing in console anymore.... – coolstoner Oct 29 '16 at 17:23
  • let me know how to mark it as the accepted answer for the question from my end – coolstoner Oct 29 '16 at 17:24
  • I'd suggest you just delete it, now that you know the answer. It is most certainly a duplicate as this has been asked many times before, and is a common issue. – adeneo Oct 29 '16 at 17:25
  • this question was wreaking havoc for a cpl of hours and you have solved my very big concern....i am able to override default properties which i couldnt.....was not debugging properly and when i see undefined was like "WTF!!!"... just changing it to `return` made my day..... – coolstoner Oct 29 '16 at 17:27

0 Answers0