admin管理员组文章数量:1401362
Normally, in Javascript, when I want to pass an anonymous/inline function as an argument to another function, I do one of the following.
someFunctionCall(function() {
//...
});
someFunctionCall( () => {
//...
});
However, I've recently inherited a codebase that uses named function as inline arguments, like this
someFunctionCall(function foo() {
//...
});
I've never seen this syntax before. The function still seems to be anonymous -- there's no foo
function defined in either the calling or called scope. Is this just a matter of style, or can using a named function (foo
above) as an anonymous function change the behavior or state of that program?
This is specifically for a NodeJS (not a browser based program) program, and I'm specifically interested in behavior specific to using functions as parameters. That said information from behavior across platforms and runtimes is wele.
Normally, in Javascript, when I want to pass an anonymous/inline function as an argument to another function, I do one of the following.
someFunctionCall(function() {
//...
});
someFunctionCall( () => {
//...
});
However, I've recently inherited a codebase that uses named function as inline arguments, like this
someFunctionCall(function foo() {
//...
});
I've never seen this syntax before. The function still seems to be anonymous -- there's no foo
function defined in either the calling or called scope. Is this just a matter of style, or can using a named function (foo
above) as an anonymous function change the behavior or state of that program?
This is specifically for a NodeJS (not a browser based program) program, and I'm specifically interested in behavior specific to using functions as parameters. That said information from behavior across platforms and runtimes is wele.
Share Improve this question edited Apr 13, 2019 at 16:49 Alana Storm asked Apr 12, 2019 at 18:24 Alana StormAlana Storm 166k95 gold badges419 silver badges621 bronze badges 7- 3 Stack traces. The name will show up in the trace if the callback throws. At least in the browser, haven't tested in node. – Jared Smith Commented Apr 12, 2019 at 18:26
- 2 tfw you suddenly realize the avoidable-hell you've been causing other developers by not doing this. Thank you! – Alana Storm Commented Apr 12, 2019 at 18:31
- 1 Also the function name would act like a self documention – Sushanth -- Commented Apr 12, 2019 at 18:33
-
1
you can also use them as a persistent yet not global object store for state.
(function me(x){ me.lastX=x; })
– dandavis Commented Apr 12, 2019 at 18:41 - 1 Also, I would suggest you understand the difference between a function and a function expression - which kind of look the same but are not entirely. – Mark Schultheiss Commented Apr 12, 2019 at 18:47
3 Answers
Reset to default 8There are at least three advantages of using named function expressions instead of anonymous function expressions.
- Makes debugging easier as the function name shows up in call hierarchy.
- The function name is accessible in the inner scope of the function, so it can be used for recursion
- The function name itself acts like a self documentation of what the function is doing instead of reading the code.
Using those "named anonymous functions" won't change the behavior but will show the function name in stack traces which is very useful. Also the function gets callable within itself that way.
I'll give an example
Case 1:
var obj = {count: 0, counter: ()=> {this.count+=1;}}
If you do console.log(obj.count) you'll get 0
Case 2:
var obj = {count: 0, counter (){this.count+=1;}}
In 2nd case if you do console.log(obj.count) value will be one.
Hope you understood by now. Lamda expressions cannot access values with reference of this object. It can only access variables with global reference.
In case 1 if you want to make it work with lamba you have to use obj.count+=1 with name has reference.
And rest of the JavaScript function implementation remains same there is not much difference.
本文标签: nodejsDifferences Between Named and Unnamed Anonymous Javascript FunctionsStack Overflow
版权声明:本文标题:node.js - Differences Between Named and Unnamed Anonymous Javascript Functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744256544a2597509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论