Goldman Sachs interview question

Explain how to implement encapsulation in Javascript

Interview Answer

Anonymous

21 Oct 2015

function test(){ var count = 0; var inc = function(){ count+=1; } var getcount = function(){ return count; } } var t = test(); alert[count); //Count not defined at global scope alert[t.getcount()); //alerts '0' t.inc(); alert[t.getcount()); //alerts '1'