0

I have a JavaScript class Test that has a private function foo that looks like this:

function Test(){}
(function()
{
    var foo = function(){
        // do stuff
        console.log('private work');
    }

    this.publicStuff = function(){
        foo();
    }

}).apply(Test);

and one that looks like this

function Test(){}
(function()
{
    function foo(){
        // do stuff
        console.log('private work');
    }

    this.publicStuff = function(){
        foo();
    }

}).apply(Test);

they both work OK and do same stuff and can even coexist like this:

function Test(){}
(function()
{
    var foo = function(){
        // do stuff
        console.log('private work VAR');
    }

    function foo(){
        // do stuff
        console.log('private work FUNC');
    }

    this.publicStuff = function(){
        foo();
    }

}).apply(Test);

But when they are together, the var foo = function() gets executed and the other one not.

What is the difference between the two ways of defining these private functions, what is the best practice and what are the advantages/disadvantages of each aproach

Bogdan
  • 1,395
  • 22
  • 31

0 Answers0