/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] diff between var myfn = function(){…} and function myfn(){…} ??

are these two ways of declaring a function merely two approaches with the same result or is there a real difference?

[code]var myFunction = function(){
// do stuff
}[/code]

…and…

[code]function myFunction(){
// do stuff
}[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledJan 19.2008 — as far as i know, their only difference are the way they're defined and that beside function statement, function literal are unnamed. however, they're supposed to behave the same way. the main difference come if you create funcion with the constructor [B]Function[/B]. each time a function is created with the constructor, it is compiled at runtime.

oops, someone posted earlier
Copy linkTweet thisAlerts:
@Jeff_MottJan 20.2008 — The site posted by dbforch says your first variation, bcamp1973, has problems in Netscape 4. I don't know anyone who still worries about NS4, so I can't confirm that, but if we ignore NS4, then the two syntaxes will do exactly the same thing.
Copy linkTweet thisAlerts:
@dbforchJan 20.2008 — You can see the difference as such, assigning a function to a variable, which in some situations might change the scope of variables inside these functions, the other assigning a function normally, as in library.

I believe through all the latest versions (1,5 1,6 1,7) of javascript, there hardly is a difference, except older versions of javascript apparently do see a difference .. (e.a. scope, etc)

Conclusion, use whatever you like, and if you notice strange behavior, you'll know the difference ?

Cheers
Copy linkTweet thisAlerts:
@bcamp1973authorJan 20.2008 — thank you! i appreciate you responses! ?
×

Success!

Help @bcamp1973 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...