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
Comments
Post a Comment