Home > Web Front-end > JS Tutorial > body text

Organize Javascript function study notes_javascript skills

WBOY
Release: 2016-05-16 15:28:32
Original
1270 people have browsed it

1. What is a function
If you need to use the same piece of code multiple times, you can encapsulate them into a function. A function is a set of statements that can be called at any time in your code. Each function is actually a short script.
For example: To complete the function of multiple sets of sums.

var sum;
sum = 3+2;
alear(sum);
 
sum = 7+8;
alear(sum);
......//不停的重复两行代码
Copy after login

If you want to implement the sum of 8 groups of numbers, you will need 16 lines of code. The more you implement, the more lines of code you will need. Therefore, we can put the code block that performs a specific function into a function and call this function directly, saving the trouble of repeatedly entering a large amount of code. Complete using functions:

function add(a,b){
  sum = a+b;//只需要写一次就可以
 };
 add2(3,2);
 add2(7,8);
 ......//只需要调用函数就可以
Copy after login

2. Define function
Define function syntax

 function 函数名(参数argument){
  函数体statements;
 }
 //function定义函数的关键字,“函数名”你为函数取的名字,“函数体”替换为完成特定功能的代码。
Copy after login
function shout(){
   var beatles = Array("John","Paul","George","Ringo");
   for (var count = 0; count < beatles.length; count++){
     alert(beatles[count]);
   }
 }
 //这个函数里面的循环语句将依次弹出对话框来显示beatles里面内容。 
 shout();调用函数,执行脚本里的动作

Copy after login

Complete the function of summing two numbers and displaying the result:

<script type="text/javascript">
  function add2(){
    sum = 3+2;
    alert()sum;
   }
   add2();
 </script>
Copy after login

3. Function call
After the function is defined, it cannot be executed automatically. You need to call it and write the function name directly in the required position.
The first case: called within the

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!