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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -