Simple analysis of function _javascript techniques in JavaScript

Source: Internet
Author: User
Tags anonymous

In scripting language JavaScript, a function is defined as a block of code that can be reused by event-driven or when it is invoked. In the standard ECMAScript of JavaScript, the function is expressed as a statement that can be run anywhere. I personally do not agree with ECMA, because the function will only be executed when the call occurs, otherwise it is a lifeless code.

Let's make a specific understanding of the function.

(a) first is the definition of the function: the definition of the ECMAScript function is the keyword function name (parameter) {principal; return (returned value)}; These four parts, but in the scripting language the definition of a function is divided into three ways:

Form 1:function function name (parameter) {principal; return value}; This is defined in the same way as ECMAScript;
Form 2:var function name =function (parameter) {principal; return value};
Form 3:var Function name =new function (parameter) {principal; return value};

There are only three ways to define a function in JavaScript, and then let's talk about the considerations and specific uses of each definition: The first definition 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 to construct an anonymous function through us, and this anonymous function is passed on to a function variable that we define, and he is very common in closures so that we can use the inner functions within the global domain to allow our functions to form a closure structure. The third way is to store our anonymous function by creating a new memory space, and assign a value to the function name we defined, which is based on our anonymous function. This approach is mainly used in prototypes.

In fact, through observation, we will find that as if these three definitions are closely related, the third one seems to be the combination of the first 2 and the third, and the fact is that you see this, so for these 3 definitions we know what to do and know when to use them respectively. Again, I'm going to say that the data types of these 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 an object. When you are exposed to the definition of objects, it is obvious that they are basically the same way of defining. Except that the function name is called an object, the form of the definition is very similar. Remember, objects are functions, and functions are objects.

(b) Several special functions
(1) anonymous function
The so-called anonymous function is a function that has no function name. This function is a bit of the case, it is difficult to call, which also guarantees security. So how do we call anonymous functions? Take a 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
A self-tuning function is also a kind of anonymous function, it is not passed as a parameter to other variables, nor its own function name, that is, it can not be called by others, can only call themselves.

The way it is written is the use of the self tuning function, this function is surrounded by two parentheses, the function of the first parenthesis is encapsulation, the anonymous function we write is encapsulated, the second parenthesis is called, and we can initialize it, just like the second self-tuning function.

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

(3) Callback function

A callback function, which is a function that passes itself as a return value or argument.

function A () and B () is the application of the callback function.

(4) Internal function

This is a sentence, is the existence of a function inside the functions.

(iii) Advantages of the function

1. A large number of repeated statements written in the function, you can repeat the call;
2. Simplify programming language, let programming become modular;
3. Optimize the structure of the Code;

(iv) Matters of concern

1. When writing a function, it can be invoked first, in writing, because JavaScript implicitly declares all of our functions and variables at the beginning of the program;
2. The function does not execute itself, only when the function call occurs, will allocate space, in order to use;
3. The function name is the same, the default execution is written in the following function;
4. Function name if there is a word composition, then the first letter lowercase, if there are more than one word, the first word after the initial capital letter;

(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 that are declared by VAR are called local variables, and if they are defined inside a function but not with the Var declaration, they are considered global variables.

We need to know that learning JavaScript language is learning a variety of functions, so for the function of understanding must be profound some, otherwise in the later learning process, we will encounter various functions of the nesting ah, what the various functions Leelawadee mixed together, will be more confused, the whole of their bag chaos. This simple article, is my understanding of the function, I hope that my humble opinion, you can understand the function some help.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.