Question: What type of function is the following and which is their best use?

```
var f = function (){
    var a = 0;
    return {
        f1 : function(){
        },
        f2 : function(param){
        }
    };
}();

```

I tried to convert it to:

```
var f = {
    a : 0,
    f1: function (){
    },
    f2: function (param){
    }
}

```

But seems does not works the same way.

Answer: 
It's just a plain old function that is invoked immediately, and returns an object which is then referenced by `f`. 

The functions referenced by object returned retain their ability to reference the `a` variable. 

No code outside that immediately invoked function can reference `a` so it enjoys some protection since you control exactly what happens to `a` via the functions you exported.

This pattern is sometimes called a *module pattern*.

---

Regarding your updated question, it doesn't work the same because `a` is now a publicly available *property* of the object.

For the functions to reference it, they'll either do:

```
f.a;

```

or if the function will be called from the `f` object, they can do:

```
this.a;

```

Like this:

```
var f = {
    a : 0,
    f1: function (){
        alert( this.a );
    },
    f2: function (param){
        this.a = param;
    }
}

f.f2( 123 );

f.f1(); // alerts 123

```

But the key thing is that `a` is publicly available. Any code that has access to `f` can access `f.a` and therefore make changes to it without using your `f1` and `f2` functions.

That's the beauty of the first code. You get to control exactly what happens to `a`.
Basically that creates a "class" - JS doesn't have classes so a class is basically a function such as your function `f`.

The interesting thing about the code you posted is that it creates a private variable `a` and two public functions `f1` and `f2`. They are public because the constructor - the outer function - returns them.

This is a common pattern for organising and encapsulating JS code.

You can read more about it [here](http://javascript.crockford.com/private.html)