2
var Alchemy = (function() {
    function Alchemy(userConf) {
      ....
      ....
    }
    return Alchemy;
}) ();

can some explain what is Alchemy variable is pointing to, i mean is that pointing to a Alchemy named function or an instance of unnamed function which has a function def of Alchemy

and what will we have if we say new Alchemy(config);

Siguza
  • 21,155
  • 6
  • 52
  • 89
pradeep
  • 53
  • 5

3 Answers3

2

Part of the confusion here is that there's essentially two "Alchemy" variables - one for each depth level of closures.

var x = 3;
(function() {
  var x;
  console.log(x) // This will be null/undefined - because it's a different X
}());

But your first theory was correct - the purpose of this block is to create a named function, Alchemy. I would guess in the ..... it does other things to initialize this function variable, like add things to its prototype.

If you call new Alchemy(config) then config will be passed in as userConf. The result of new Alchemy call will be a new object of type Alchemy, and any references to the keyword this inside of the .... area will affect said object.

Katana314
  • 8,429
  • 2
  • 28
  • 36
0

var Alchemy is going to be equal to a function named Alchemy that accepts a userConf parameter.

d0nut
  • 2,835
  • 1
  • 18
  • 23
0

Variable Alchemy will be the Alchemy function that is returned in the closure. One of the reasons this would be done would be to hide any other variables within that function from global scope.

var Alchemy = (function() {
    var someVar = '';

    function Alchemy(userConf) {
      ....
      ....
    }
    return Alchemy;
}) ();

In this example, someVar will not be accessible outside of the closure.

Mychal Hackman
  • 319
  • 1
  • 8