Js collection: function literal

Source: Internet
Author: User

The function literal is composed of four parts. The first part, the second part of the keyword function, the function name, but dispensable. The third part is the parameters included in the brackets. Of course, the parameters are also dispensable and should not be fewer. The fourth part is a set of statement blocks enclosed in braces, that is, the specific code to be executed by the function. Of course, it is okay if you do not write the code. {} is required. At first glance, isn't this the definition of a function? In fact, we mentioned a limitation, a function expression. When a function is used as an expression, it can be divided into these four parts. The second part is the function name, but it is optional. If the function is declared, an error is returned if the function name is missing. So the function expression is the literal value of the function mentioned here. For example, the text function () {} is written in accordance with the preceding four statements, but it is not an expression. A syntax error is reported during running. It can be executed normally only when it is used as an expression. Text (function () {}); var a = function () {}; var obj = {fn: function () {}}; no problem in writing this code .. Let's take a look at the text var fn = function test () {}; is this a function declaration? This is also the function literal. test is the name of the function, but it is only visible to the function body. It cannot be called outside. Run var fn = function test () {console. log (test) ;}; console. log (fn); fn (); console. log (test); // report that test does not define the function name here, which can be used internally. For example, it is very convenient for recursive calls. Some may say why not use arguments. callee. In many cases, we can see that arguments is a double-edged sword and a performance killer. if you can avoid it, try to avoid it. Adding a function name makes it easy to call and has excellent performance. It also benefits project compression. Why not?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.