Function
What is a function ? a function is an event-driven, or reusable block of code that executes when he is invoked. . I don't think I understand . , a function is a block of code that can accomplish a function. .
Look at a case :
<! DOCTYPE html>
<script>
function MyFunction ()
{
Alert ("Hello world!");
}
</script>
<body>
<button onclick= "myFunction ()" > click here </button>
</body>
Wait , What we said earlier . declaring a function with the functions keyword you should know ? don't be ridiculous. , you must know that. .
JS function Syntax
A function is a block of code wrapped in curly braces , preceded by a keyword function:
function functionname ()
{
Here is the code to execute
}
What happens when a function is called ? you should know how to call it. !
A function can be called directly when an event occurs, such as when a user taps a button , and can be called from any location by JS .
Hint : is JS sensitive to case ? so function How to write a keyword ?
Calling a function with parameters
When you call a function , You can think of passing values , which become parameters .
These parameters can be used in functions .
You can send people a number of parameters separated by commas (,) :
Function method (Argument1,argument2,... argumentn)
{}
When you declare a function , Declare the argument as a variable :
function MyFunction (VAR1,VAR2)
{
The code to execute
}
Variables and parameters must appear in a suppressed order . The first variable is the given value of the first parameter passed , etc. .
Example :
<script>
Function Method (Name,age)
{
Alert ("Name:" +name+ ", Age:" +age);
}
Method ("Syx", 22);
</script>
In fact, this simple call is meaningless , right . mainly to have a kind of cattle X the effect , A function can be triggered by an event . just like this . :
<script>
Function Method (Name,age)
{
Alert ("Name:" +name+ ", Age:" +age);
}
</script>
<body>
<button onclick= "Method (' Syx ',") "> click I try </button>
</body>
function with return value
Sometimes , We would want the function to return the value to the place where it was called.
By using the return statement, you can implement the
When you use the return statement , the function stops execution and returns the specified value .
Grammar
function Method ()
{
var x=5;
return x;
}
The function above will return 5.
Note : the entire JS does not stop executing , just the function . JS will continue to execute code from where the function is called .
The function call will be replaced by the return value :
var value=method ();
the value of the values variable is 5, which is the value returned by the function method () .
You can use the return value even if you do not save it as a variable .
document.getElementById ("Demo"). Innerhtml=method ();
The InnerHTML of the "demo" element will be 5, which is the value returned by the function "Method ()" .
You can make the return value based on the arguments passed to the function :
<p> This example invokes a function that performs a calculation and then returns the result:</p>
<p id= "Demo" ></p>
<script>
function MyFunction (A, B)
{
return a*b;
}
document.getElementById ("Demo"). Innerhtml=myfunction (4,3);
</script>
</body>
You can also use the return statement When you just want to exit the function . The return value is optional :
function MyFunction (A, B)
{
if (a>b)
{
return;
}
X=a+b
}
If a>b, the above code exits , and the value of a+b is not computed .
Local JS variable
The variable declared inside the Js function ( using var) is a local variable , so it can only be accessed inside the function . ( the scope of the variable is local ).
You can have a local variable with the same name in a different function , because only a function that declares the variable can recognize it .
As soon as the function is complete , the local variable is deleted .
Global JS variable
Variables declared outside of a function are global variables , and all scripts and functions on a Web page can access it .
the lifetime of the JS variable
The lifetime of the JS variable starts at the time they are declared .
Local variables are deleted after the function is run .
Global variables are deleted after the page is closed .
Assigning a value to an undeclared JS variable
If you assign a value to a variable that has not been declared , the variable will be automatically declared as a global variable . Case :
Value= "Hello,world";
Will life a global variable value. in time he executes within the function .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JS Learning Day----function