In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the use of anonymous functions in JavaScript, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
This article comes from a question from Zhihu:
Functions are also determined at run time, so why is b an anonymous function?
I tinkered with this seemingly simple question in the browser for more than an hour. And it seems to have found two suspected bug of FireFox/Chrome Devtools. In the ES specification, there is an internal function IsAnonymousFunctionDefinition () to determine whether a function is anonymous, but this function is only used in the specification and cannot be called through JS code, and many JS engines implement this function internally (not mandatory). When we talk about anonymous functions, there are actually two different meanings: the name property of a function without a name is "anonymous" because the name of the function is inherited from Function.prototype.name, so the name property of a function without a name is an empty character. In most debugging tools, unnamed functions are displayed as anonymous functions that we often call anonymous functions. Let's rewrite the code in the title: var a = () = > () = > {throw new Error ('foo')} whether it is a () () or var b = a (); b (), executed in Chrome:
As you can clearly see, this function is anonymous. (ignore the bottom anonymous function, which is Global Scope) the call stack in FireFox is:
This is so fascinating. No matter how you call the b function, a should not appear in the call stack, which I think can be regarded as a bug of FireFox. If you use new Function to define a function, there are no angle brackets in the error stack. Because according to the specification, functions defined using Function have a name, and the name is "anonymous". Just like a person named John Doe, not because he does not have a first name, but because his surname is Wu, his name is John Doe. Firefox has a slight advantage in dealing with this: var a = new Function ('throw new Error ("bar")) a () FireFox screenshot:
Let's take another look at Chrome Devtools:
I'm a little confused. Maybe I got the call stack dump out of the engine. I think this can also be thought of as a bug. The puzzle of the subject is why the function name of var a = () = > () = > 1 is a, while var b = a () is an anonymous function. The simple explanation is that var a = () = > 1 contains two phases, the arrow function definition and the assignment operation. All arrow functions are anonymous. In the definition of arrow function, the hasName property inside the specification is directly set to false. Then IsAnonymousFunctionDefinition (expr) determines that the function is anonymous: If IsFunctionDefinition of expr is false, return false. Let hasName be HasName of expr.If hasName is true, return false. Return true. When assigning a value, it will first determine whether the right value is an anonymous function. If so, reset the function name. Let's look at another statement: var b = a (). This also includes an assignment operation, as well as a function call operation. The name property, the function name, is reset for the variable (rval) only when the anonymous function (static semantics) performs the assignment. The a () here is just a function call, not a function definition, so the function name will not be reset. Similar: var a = (1, () = > 'foo') a = [() = > 1] [0] var a = eval (' () = > 1')
So much for sharing the use of anonymous functions in JavaScript. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.