Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible To Call A JavaScript Function Inside An Immediately-Invoked Function Expression

I'm using jQuery and have a function wrapped inside an immediately-invoked function expression like so:

<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    (function ($) {

        var message = 'x called';

        function x() {
            alert(message);
        }
    })(jQuery);

    x();
</script>

This will result is an error since the function "x" is not defined outside the immediately-invoked function expression. Is there any way to call the function "x" outside the immediately-invoked function expression?

like image 976
Halcyon Avatar asked Jan 09 '13 20:01

Halcyon


People also ask

How do you call a function immediately in JavaScript?

An immediate function is one that executes as soon as it is defined. Creating an immediate function is simple: you add the open/close parentheses after the closing curly bracket, and then wrap the entire function in parentheses. That's it!

Do immediately invoked functions wait for a function call to be executed?

An Immediate-Invoked Function Expression (IIFE) is a function that is executed instantly after it's defined. This pattern has been used to alias global variables, make variables and functions private and to ensure asynchronous code in loops are executed correctly.

Why JavaScript immediately invoked function expression?

It is a JavaScript function that runs as soon as it defined. An IIFE (Immediately Invoked Function Expression) can be used for avoiding the variable hoisting from within the blocks. It allows the public access to methods while retaining the privacy for variables defined in the function.

Which of the following is the correct example of immediately invoked function expression?

For example, let us take the following example where the IIFE is defined within a function and will only be immediately invoked if we call the Parent Function. console. log( "Welcome to" ); // This will be executed after executing the previous log.


2 Answers

Only if you expose the function in some way. For example, you can return it from the outer function:

var x = (function ($) {

    var message = 'x called';

    function x() {
        alert(message);
    }

    return x;
})(jQuery);

x();

Or, similarly, you can return it on an object:

var obj = (function ($) {

    var message = 'x called';

    function x() {
        alert(message);
    }

    return {"x": x};
})(jQuery);

obj.x();

Functions and variables declared inside of a function are not directly reachable from outside of that function, unless you provide some means of accessing them by returning something, or giving a reference to a variable declared outside of that function.

like image 136
cdhowie Avatar answered Oct 05 '22 05:10

cdhowie


Make a namespace for other classes or functions you might want to do this with. You don't want to continually pollute the global namespace but there's no reason you can't make one namespace that's global and put your individual things underneath that:

(function($){
  window.MyNamespace = function(){};
  var message = "Something here";

  $.extend(MyNamespace, {
    x: function(){
      alert(message);
    }
  });
})(jQuery)

MyNamespace.x()
like image 32
nzifnab Avatar answered Oct 05 '22 05:10

nzifnab