javascript - invoke function wrapped inside of anonymous function -


to avoid variable collision , "everything" javascript experts should use self invoking function 1 below. how can reference outside. or example how can invoke foo function inside it?

onclick="foo()" // doesn't work outside !!!

<div style="width: 500px; height: 500px; background: red;" id="dfg" onclick="foo()"></div>  <script>  (function () { function foo(){ console.log("everything ok") } }()); </script> 

well can add method window object.

(function (global) {      global.foo = function(){         console.log("everything ok")     }  }(window));  foo(); // ok 

demo


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -