Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. (function() {
  2. ...
  3. code
  4. ...
  5. })();
  6.  
  7. function outerFunction() {
  8. function innerFunction() {
  9. // code
  10. }
  11. }
  12.  
  13. var globalVariable;
  14.  
  15. function someFunction() {
  16. var localVariable;
  17. }
  18.  
  19. function globalFunction() {
  20.  
  21. var localFunction1 = function() {
  22. //I'm anonymous! But localFunction1 is a reference to me!
  23. };
  24.  
  25. function localFunction2() {
  26. //I'm named!
  27. }
  28. }
  29.  
  30. (function() {
  31. var private_var;
  32.  
  33. function private_function() {
  34. //code
  35. }
  36. })()
  37.  
  38. var myPlugin = (function() {
  39. var private_var;
  40.  
  41. function private_function() {
  42. }
  43.  
  44. return {
  45. public_function1: function() {
  46. },
  47. public_function2: function() {
  48. }
  49. }
  50. })()
  51.  
  52. (function(jQ) { ... code ... })(jQuery)
  53.  
  54. var someFunction = function(){ console.log('wagwan!'); };
  55.  
  56. (function() { /* function scope starts here */
  57. console.log('start of IIFE');
  58.  
  59. var myNumber = 4; /* number variable declaration */
  60. var myFunction = function(){ /* function variable declaration */
  61. console.log('formidable!');
  62. };
  63. var myObject = { /* object variable declaration */
  64. anotherNumber : 1001,
  65. anotherFunc : function(){ console.log('formidable!'); }
  66. };
  67. console.log('end of IIFE');
  68. })(); /* function scope ends */
  69.  
  70. someFunction(); // reachable, hence works: see in the console
  71. myFunction(); // unreachable, will throw an error, see in the console
  72. myObject.anotherFunc(); // unreachable, will throw an error, see in the console
  73.  
  74. var someFunction = function(){ console.log('wagwan!'); };
  75.  
  76. var myMainFunction = function() {
  77. console.log('start of IIFE');
  78.  
  79. var myNumber = 4;
  80. var myFunction = function(){ console.log('formidable!'); };
  81. var myObject = {
  82. anotherNumber : 1001,
  83. anotherFunc : function(){ console.log('formidable!'); }
  84. };
  85. console.log('end of IIFE');
  86. };
  87.  
  88. myMainFunction(); // I CALL "myMainFunction" FUNCTION HERE
  89. someFunction(); // reachable, hence works: see in the console
  90. myFunction(); // unreachable, will throw an error, see in the console
  91. myObject.anotherFunc(); // unreachable, will throw an error, see in the console
  92.  
  93. var italianSayinSomething = function(){ console.log('mamamia!'); }();
  94.  
  95. var someFunction = function(){ console.log('wagwan!'); };
  96.  
  97. var myMainFunction = function() {
  98. console.log('start of IIFE');
  99.  
  100. var myNumber = 4;
  101. var myFunction = function(){ console.log('formidable!'); };
  102. var myObject = {
  103. anotherNumber : 1001,
  104. anotherFunc : function(){ console.log('formidable!'); }
  105. };
  106. console.log('end of IIFE');
  107. }();
  108.  
  109. someFunction(); // reachable, hence works: see in the console
  110. myFunction(); // unreachable, will throw an error, see in the console
  111. myObject.anotherFunc(); // unreachable, will throw an error, see in the console
  112.  
  113. function(){ console.log('mamamia!'); }();
  114.  
  115. (function(){ console.log('mamamia!'); })(); // live demo: jsbin.com/zokuwodoco/1/edit?js,console.
  116.  
  117. +function(){ console.log('mamamia!'); }(); // live demo: jsbin.com/wuwipiyazi/1/edit?js,console
  118.  
  119. -function(){ console.log('mamamia!'); }(); // live demo: jsbin.com/wejupaheva/1/edit?js,console
  120.  
  121. var someFunction = function(){ console.log('wagwan!'); };
  122.  
  123. (function() {
  124. console.log('start of IIFE');
  125.  
  126. var myNumber = 4;
  127. var myFunction = function(){ console.log('formidable!'); };
  128. var myObject = {
  129. anotherNumber : 1001,
  130. anotherFunc : function(){ console.log('formidable!'); }
  131. };
  132. console.log('end of IIFE');
  133. })();
  134.  
  135. someFunction(); // reachable, hence works: see in the console
  136. myFunction(); // unreachable, will throw an error, see in the console
  137. myObject.anotherFunc(); // unreachable, will throw an error, see in the console
  138.  
  139. (function() {
  140. var myNumber = 4; /* number variable declaration */
  141. var myFunction = function(){ /* function variable declaration */
  142. console.log('formidable!');
  143. };
  144. var myObject = { /* object variable declaration */
  145. anotherNumber : 1001,
  146. anotherFunc : function(){ console.log('formidable!'); }
  147. };
  148. myOtherFunction = function(){ /* oops, an assignment instead of a declaration */
  149. console.log('haha. got ya!');
  150. };
  151. })();
  152. myOtherFunction(); // reachable, hence works: see in the console
  153. window.myOtherFunction(); // works in the browser, myOtherFunction is then in the global scope
  154. myFunction(); // unreachable, will throw an error, see in the console
  155.  
  156. (function() {
  157.  
  158. var x = 2;
  159.  
  160. // do stuff with x
  161.  
  162. })();
  163.  
  164. (function($) { ...code...})(jQuery);
  165.  
  166. navigator.html5={
  167. canvas: (function(){
  168. var dc= document.createElement('canvas');
  169. if(!dc.getContext) return 0;
  170. var c= dc.getContext('2d');
  171. return typeof c.fillText== 'function'? 2: 1;
  172. })(),
  173. localStorage: (function(){
  174. return !!window.localStorage;
  175. })(),
  176. webworkers: (function(){
  177. return !!window.Worker;
  178. })(),
  179. offline: (function(){
  180. return !!window.applicationCache;
  181. })()
  182. }
  183.  
  184. (function() {
  185. 'use strict';
  186.  
  187. //Your code from here
  188. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement