-2

Consider:

var a = {
   eat: function () {
       console.log('aa')
   }
}
var b = Object.create(a);
console.log(b.eat());

The output of that lines in Chrome is following

aa script.js:3
undefined script.js:7

First line is obvious, but where does second line of output, "undefined", comes from ?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
tchelidze
  • 8,050
  • 1
  • 29
  • 49

2 Answers2

4

You are trying to log the return value of the eat method, but it does not have a return statement so an undefined value is logged instead.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

while b.eat() function is executing its log the the value "aa" but its has nothing to return so returning undefined

try to return something from function to avoid undefined

Satish Kumar sonker
  • 1,250
  • 8
  • 15