What is the point of using a named function expression? [duplicate]

2024/2/27 8:16:23

I'm going through this blog about the difference between function declarations and function expressions.

It gives these two examples. They call the first an "anonymous function expression" and the second a "named function expression."

// anonymous function expression
var a = function(){return 3;
}// named function expression
var b = function bar(){return 3;
}

I tested these two in Chrome's JS console and I see the following:

a()
=> 3b()
=> 3bar()
=> bar is not defined

My question is: In the second function expression declaration, what is the point of "bar"? In general, why does one ever use a named function expression?

Answer

Some people prefer to do it like this because if errors occur, your functions have names. It's mostly a matter of preference and how often you have trouble with unnamed functions.

You don't normally see it used in a var declaration, but instead when declaring callbacks:

callbackFunction(function success() { ... }, function fail() { ... })

That way you know which argument is which, they're labelled, and if one of them fails you get a precise indication of which one broke.

http://en.ppmy.cn/q/42406.html

Related Q&A

Using npm modules with ES6 and Future of npm/bundlers

I use ES6 for a while and have no experience with npm or node.js. I want to use some npm modules in my project using ES6. I tried following and got an error import {axios} from ./axios.js;The requested…

Alternative for the ActiveX object for the other browsers excluding IE?

Can anyone help me as i am stuck in creating a file(temporary) with extension(say .csv) in the local disk, i tried using ActiveX object but it works fine only in internet explorer,got a error when i tr…

Capture keypress in Javascript (Google Docs)

Im trying to write a little greasemonkey script/bookmarklet/what have you for Google Docs. The functionality Id like to add needs a keypress/keyup/keydown event handler (one of those three). Unfortunat…

What is the proper to write a global const in javascript es6?

What is the right way to write a global const in a script in javascript es6 and use it in some other script?

JS FTP web directory filelist

Is it possible to use Javascript to invoke a FTP call to a specified URL and retrieve all files in this webdirectory?...

dynamically loaded js function does not appear in Firebug js debugger

There is a page1.html (I open it in browser):<div id="content"> </div> <script type="text/JavaScript">jQuery.ajax({type : "GET",url : page2.html,dataType…

How to position a fixed-location element on IOS browser when zoomed?

I have a nice little React drag-drop library that works for mouse- and touch systems. For touch, it grabs the touch location via clientX and clientY (e.targetTouches[0].clientX, e.targetTouches[0].clie…

JQuery Mobile file upload [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Google Maps Android Studio - how disable the map controls?

Im working in Android Studio with MapsActivity. Everything works fine, but how do I disable the map controls that show up on top of the Google map?Here is my code:public class MapsActivity extends Fra…

Is it possible to hide URL bar in iOS 9 (Safari browser) by using JavaScript?

I want to hide the URL bar from my single-page application by using JavaScript.I tried to set bigger height of the body element and then perform:window.scrollTo(0, 0);But it doesnt work. The URL bar is…