Organize JavaScript functions learn notes _javascript tips

Source: Internet
Author: User

1. What is a function
If you need to use the same piece of code more than once, you can encapsulate it into a function. A function is a set of statements that are allowed to be invoked at any time in your code. Each function is actually a short script.
such as: To complete the number of groups and functions.

var sum;
sum = 3+2;
Alear (sum);
 
sum = 7+8;
Alear (sum);
.//Repeated two lines of code

If you want to implement the 8 group number and, you need 16 lines of code, the more implementation, the more lines of code. So we can put the code block that completes a certain function into a function, call this function directly, eliminate the trouble of repeatedly entering a lot of code. Use function to complete:

function Add (a,b) {
  sum = a+b;//only need to write once to
 };
 ADD2 (3,2);
 ADD2 (7,8);
 .../only need to call the function to

2. Defining functions
Defining function syntax

 function function name (parameter argument) {
functional body statements;
 }
 function defines the key word for functions, "function name" is the name you take for the function, and "function body" is replaced by the code that completes the specific function.
function Shout () {
   var beatles = Array ("John", "Paul", "George", "Ringo");
   for (var count = 0; count < beatles.length; count++) {
     alert (Beatles[count]);
   }
 The loop inside the function will pop up a dialog box to display the contents of the Beatles. 
 shout (); Calling function, executing the action in the script

Complete the ability to sum two numbers and display the results:

<script type= "Text/javascript" >
  function Add2 () {
    sum = 3+2;
    alert () sum;
   }
   ADD2 ();
 </script>

3. Function call
After the function is defined, it cannot be executed automatically, it needs to be called and the function name is written directly in the desired position.
In the first case: called within the <script> tag.

<script type= "Text/javascript" >
   function Add () {
     sum = 1+1;
     alert (sum);
  }
   Add ();//Call function, write function name directly.
</script>

Second scenario: Call in an HTML file, such as a defined function by clicking a button.


function Add2 (x,y) {
   sum = x + y;
   document.write (sum);
 X and Y are the two parameters of the function, and when we call the function, we can pass the two actual addends to the function by using these two parameters.

The following functions are implemented:
defines a function that implements three numbers and the function is named ADD3.
Calculates the and of 42 numbers in 5, 8, 3/7, 1, and three groups.

 <script type= "Text/javascript" >
   function add3 (x,y,z) {
   sum = x + y +z;
     document.write (x+ "," +y+ "," +z+ "and:" +sum+ "<br/>);
   }
   ADD3 (5,8,3);
   ADD3 (7,1,4); 
 </script>

5, return the value of the function
A function can not only receive data (in the form of arguments), but it can also return data. We can completely create a function and let it return a value, a string, an array, or a Boolean value. This is the need to use the return statement.

 function Multiply (num1,num2) {
   var total = num1*num2;
   return total;
 }

Output the results by "document.write", now using the function output
All we have to do is change the "document.write" line to the following code:

 function Add2 (x,y) {
   sum = x + y;
   return sum; Returns the value of the function, which is called the return value.
 }
 The return value of the calling function can also be stored by a variable:
 result = ADD2 (3,4);//The value in the performance variable is 7 after execution.

Example: The following function has only one parameter (a Fahrenheit temperature value) and it returns a numerical value (Celsius temperature of the same temperature):

 <script type= "Text/javascript" >
   function Converttocelsius (temp) {
     var result = temp-32;
     result = result/1.8;
     return result;
   }
   The real value of functions is that we can also use them as a data type, which means you can assign a function's invocation result to a variable:
   var Temp_fahrenheit =;
   var Temp_celsius = Converttocelsius (Temp_fahrenheit);
   alert (Temp_celsius);
 </script>

In this example, the value of the variable Temp_celsius will be 35, which is returned by the Converttocelsius function.
When I name a variable, I use an underscore to separate the words; when I name a function, I start with the second word to capitalize the first letter of each word (that is, the hump-naming method).
1), the scope of the variable
A variable can be either global or local.
global variable can be referenced anywhere in the script. Once you have declared a global variable in a script, you can refer to it from any location in the script------including the internal------of the function. Global variables are scoped to the entire script.
A local variable is only in the interior of the function that declares it, and cannot be referenced outside of that function. The scope of a local variable is limited to a particular function.
You can explicitly set the scope for a function variable with the var keyword.
If Var is used in a function, that variable will be treated as a local variable that exists only in the context of the function, and conversely, if Var is not used, the variable will be treated as a global variable, if a global variable with the same name already exists in the script, This function will change the value of that global variable.
Example:

function Square (num) {total
   = Num*num;
   return total;
 }
 var total = m;
 var number = square;
Alert (total);

This is wrong, the value of number is 400, but here alert pops up the value of total, which should be 50.

This code will inevitably cause the value of the global variable total to change.

The value of the global variable total becomes 400. My intention was to have the square () function return only the squared value it computed to the variable number, but because the total variable in the function was not explicitly declared as a local variable, This function also changes the value of the global variable whose name is the same as total.

It is correct to write this function as follows:

 function Square (num) {
   var total = num*num;
   return total;
 }
 var total = m;
 var number = square;
 Alert (total);
 </script>

Correct result:

The above is about the JavaScript function of the learning notes, but also involves some variables of knowledge, I hope to help you learn.

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.