October 2014: singles' Javascript Interpretation Section 3, October 2014
After getting started in Section 1 and the operators in section 2, we can now learn Javascript Functions. Of course, no matter what programming language you have learned before, there will be a function concept, if you are learning Pascal, there will be a "process" concept, but Javascript is not that complicated.
***********
1. The so-called function is a collection of code. We encapsulate it in a form and use it as a whole. Then we have the concept of a function.
2. in js, we first use the keyword funtion to declare a function, followed by the function name, followed by a parentheses. Some parameters can be written in parentheses, followed by a pair of braces, the function body in braces is the function code. The example is as follows:
Function xin () {alert ("xin Xing ");}
3. we have defined a very simple function above. Its function is to pop up a window and display the word "Xin Xing" in the window. This alert is also a function, we called it here.
4. A function can have parameters. Because Javascript is a weak language, it does not need to specify the parameter type when passing parameters. You can directly use the parameter name, separate multiple parameters with commas.
5. For example, we create an html file with the following content:
<Html>
Create a new my. js file and add the following content:
Function xin (name, age) {alert ("name:" + name + "age:" + age );}
6. Now we find that if we click the button, the corresponding display information will pop up.
7. like most programming languages, a function can return values. We use return statements to return data. When a function has a return statement, the function automatically stops execution, and the execution data is returned.
8. Sample Code:
function getvar(){ return 4;}
9. The above is an example of a typical function return value. It has no other functions, that is, simply returning a data.
************
1. the variables declared outside the function are called global variables. All js scripts on the webpage can access them, and the global variables will be deleted after the page is closed.
2. the variable defined by the var keyword in the function is a local variable and can only be used within the function. After the function is executed, the variable is deleted. When the function is called again, it will be created again.
3. A Javascript variable has started its own life cycle since it was declared.
4. If we assign a value to an undeclared variable, the variable is automatically used as a global variable, even if it is declared in the function.
*************
1. This section is relatively simple. We just introduced the functions and scopes of Javascript.
2. There is also a closure concept, which is not difficult. We will introduce it later.