javascript - What's the difference between declaring a function by itself or attaching it to window? -


sorry, title doesn't make sense, want know difference between:

window.myfunc = function(){}

and

function myfunc(){}

with jsfiddle, functions seem work when attached window. i'm assuming how it's set read javascript , html separately, there otherwise no difference?

the first 1 garanties function global , available everywhere.

the second 1 same. second 1 take scope of first function embed it.

first case:

function foo()  {    window.bar = function (){} } foo(); bar(); // valid; 

second case

function foo()  {     function bar(){} } foo(); bar(); // fail 

i hope makes sense you


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -