javascript - Invoking function of a returned object from function prints undefined -
var unique = function(){ var n=0; return function(){ return { inc : function(){ n++; console.log(n); } }; }; }; console.log(unique()().inc());
the code above prints 1 , undefined reason undefined gets printed ?
because asked log value returned inc
, , doesn't return anything.
if don't want print anything,
console.log(unique()().inc());
should be
unique()().inc();
if expect new value of n
printed,
inc : function(){ n++; console.log(n); }
should be
inc : function(){ n++; console.log(n); return n; }
Comments
Post a Comment