two ways of IIFE(immediately-invoked-function-expression)-JS

来源:互联网 发布:艺恩咨询数据库 编辑:程序博客网 时间:2024/05/21 15:05
one way:

file1, IIFE.js

(function() {    var jQuery = {        myAlert:function myAlert(){            alert("IIFE Test");        }    };    window.jQuery = jQuery;})();

file 2,IIFE-test.html

<html>    <script type="text/javascript" src="IIFE.js"></script>    <script type="text/javascript">        jQuery.myAlert();    </script></html>


another way:

js codes,

var jQuery = function(){      var myAlert = function(){           alert("test");          }      return{           myAlert : myAlert      };}();

omit html codes.


more:
1. http://learn.jquery.com/javascript-101/scope/

2. http://benalman.com/news/2010/11/immediately-invoked-function-expression/