Proper function conventions for JavaScript -


this question has answer here:

i've been doing codecademy javascript lesson, , they're telling use functions this:

var functionname = function(parameters) {}; 

i've done javascript before , i've done this:

function myfunction(parameters){} 

whats correct way? there difference? when should use either?

thanks!

it depends, in general normal function want use second way:

function myfunction(parameters) { } 

you can assign function variable using first way if want , mixing , matching.

// assign anonymous function functionname var functionname = function (parameters) { }  // assign pointer myfunction functionname var functionname = myfunction 

these different things though, imagine for loop containing these, first 1 make new function every iteration second reference existing function.

for (var = 0; < 10; i++)     // creates 10 functions     var functionname = function (parameters) {     }      // uses existing function     var functionname = myfunction } 

it depends on you're doing though, example can use inline functions fine.

window.onload = function () { }; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -