Analysis of functions in JavaScript

Source: Internet
Author: User
Tags closure

In scripting language JavaScript, the definition of a function is an event-driven block of code that can be reused when it is called. In JavaScript's standard ECMAScript, the function is expressed as a statement that can be run anywhere. I personally do not agree with the ECMA argument, because the function is only executed when the call occurs, otherwise it is a lifeless code. Let's take a concrete understanding of the cognitive function.

(a) First is the definition of the function: the definition of the ECMAScript function is the key function name (parameter) {principal; return (returned value)}; The four parts, but the definition of a function in a scripting language is defined in three ways:

Form 1:function function name (parameter) {body; return value}; This is defined in the same way as ECMAScript;

Form 2:var function name =function (parameter) {body; return value};

Form 3:var Function name =new function (parameter) {body; return value};

There are only three ways to define functions in JavaScript, and then there are the caveats and specific uses of each definition: the first is the simplest and easiest to understand, and he simply defines a function for us to use in the next programming. The second way is by constructing an anonymous function and passing the anonymous function to a function variable that we define, which is very common in the closure, so that in the global domain, we can use the inner layer function to make our function to form a closure structure. The third way is to store our anonymous function with a new memory space by new and assign a value to the function name that we define, which is created based on our anonymous function. This approach is mainly used in prototypes.

In fact, through observation, we will find that it seems that the three definitions are closely related to each other, feeling that the third first way seems to be the combination of the previous 2 and the third, and the real thing is what you see, so for these 3 definitions we know the way, and know when they are used separately. Again, I would like to say that the data types of the three definitions are based on the basic type, and the data based on the object type is the same, in the object-based data type, we call the function object. When you touch the definition of an object, it is obvious that they are basically the same way of defining it. Only the function name is called an object, and the form of the definition is very similar. Remember that an object is a function, and a function is an object.

(ii) several special functions

(1) anonymous function

The so-called anonymous function is a function that does not have a function name. A bit of this function means that it's very difficult to call, which guarantees security. So how do we invoke anonymous functions? Let's look at an example:

By passing the anonymous function as an argument to the add variable, we can call our anonymous function.

(2) Self-tuning function

The self-tuning function is also an anonymous function, it is not passed as a parameter to other variables, and does not have its own function name, that is, it can not be called by others, can only call themselves,

The way the graph is written is the use of self-tuning functions, this function is surrounded by two parentheses, the function of the first parenthesis is encapsulated, the anonymous function we write in which the second parenthesis is called, we can also give him initialization, as the second self-tuning function of the same way.

In fact, the self-tuning function is mainly used to implement a one-time function, that is, in the execution cycle only run once, so when we do the initialization of the Web page, we can consider using the self-tuning function.

(3) Callback function

A callback function is a function that takes itself as a return value.

Functions A () and B () are the applications of the callback function.

(4) Internal function

This is a sentence, which is a kind of function that exists inside the function.

(c) Advantages of the function

1. A large number of repeated statements written in the function, you can repeat the call;

2. Simplify the programming language and make the programming become modular;

3. Optimize the code structure;

(iv) Precautions

1. When writing a function, it can be called first, in writing, because JavaScript implicitly declares all of our functions and variables at the beginning of the program;

2. function does not execute itself, only in the event of a function call, space will be allocated to use;

3. Function name in the same time, the default execution is written in the following function;

4. Function name if there is a word composition, the first letter lowercase, if there are more than one word composition, the first word after the other words capitalized;

(v) The Variable field of the function

1. Global variables

Variables written in the global domain are called global variables;

2. Local Variables

Variables that are defined inside a function and declared with VAR are called local variables, and if they are defined inside a function but are not declared with Var, they are considered global variables.

We have to know clearly, learning JavaScript language is to learn a variety of functions, so the understanding of the function must be profound, otherwise in the course of learning, we will encounter a variety of functions of nesting ah, what the various functions of melted together, will be more confused the clue, the whole of their own bag chaos. This simple article, is my understanding of the function, I hope that my humble opinion, you can understand the function of some help ....

Analysis of functions in JavaScript

Related Article

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.