Section 3 of singles' Javascript interpretation in 2014

Source: Internet
Author: User

Section 3 of singles' Javascript interpretation in 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:

<Script src = "my. js"> </script>    Xin Xing    Xiaoqian 

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.

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.