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

javascript - Iterate over array and calculate average values of array-parts -

iphone - Using nested NSDictionary with Picker -

objective c - Newbie question -multiple parameters -