4

can someone please explain what the difference is between these two functions?

(function(Engine, $, undefined) { //hidden from scope
    //public function below
    Engine.Init = function() {
        console.log("IM PUBLIC");
    }

    //anonymous functions below
    function Login() {
        console.log("IM PRIVATE");
    }
})( window.Engine = window.Engine || {}, jQuery );

Specifically, I'd like to know why Engine.Init() is available in the Console but Login isn't.

4 Answers 4

7

Init is a property of the Engine object that refers to a function.
You can access it like any other property.

Login is a local variable within the anonymous, "immediately invoked function expression" (IIFE); like other local variables, its name is only visible within the declaring function

Sign up to request clarification or add additional context in comments.

4 Comments

One question I would like to ask you here though it might not be related to this question: what is the difference between (function(){}) versus function(){}? Does this has something to deal with anonymous function or scope or what is it?
It's just a syntactic embellishment to make sure the function definition is immediately callable. See benalman.com/news/2010/11/…, under "An aside: functions, parens, and SyntaxErrors."
@lmjohns3: You pointed me to the absolutely right direction. Thanks for the link.
Lovely explanation and all the better for that link! Cheers guys!
2

Engine is global because of the parameters:

(window.Engine = window.Engine || {}, jQuery)

and available in the global namespace, if you did:

Engine.Login = function(){}

That would be available globally.

The function Login is only available inside the scope of the anonymous self executing function.

Comments

2

There is not really a difference between the functions themselves. The only difference is that the first function is assigned to a property of an object (Engine.init) defined in global scope (window.Engine), whereas the second function is defined locally inside the immediately invoked function expression (IIFE).

Here is a equivalent, but simpler example:

function foo() {
    // 1
    window.globalFunc = function() {
        // global/public
    }

    // 2
    function localFunc() {
        // local/private
    }
}

foo();

Because the first function is explicitly assigned to a global variable, it can be accessed outside of foo, after foo was executed. localFunc is not exported and hence local to foo, i.e. it cannot be accessed and doesn't exist outside of foo.

Comments

0

This thing

function(Engine, $, undefined) {
...
}

is actually a closure. So everything define inside that function is available only in that scope. When you do

Engine.Init = ...

You create a property which is attached to Engine object. In your case Engine is a global object, which means that you have an access to it via the console.

3 Comments

"So everything define inside that function is available only in that scope." That has nothing to do with closures, that's how every function works. Although technically every function is a closure in JS, the issue here has nothing to do with closures.
Hm ... every function in JavaScript (as every other thing) is an object. We could say that the properties of the object are public variables. If you add "var something = ..." inside the function that thing is a private variable available only for the closure's scope. Isn't that true?
You could say that, but it doesn't matter whether the function is a closure or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.