Function
What is a function ? a function is an event-driven or reusable block of code that runs when it is invoked . not expected to be clear. , the individual feels that the function is a block of code that can complete a function .
Look at a case :
<! DOCTYPE html>
<script>
function MyFunction ()
{
Alert ("Hello world!");
}
</script>
<body>
<button onclick= "myFunction ()" > click here </button>
</body>
Wait , that's what I said. declaring a function keyword with functions do you always 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 the keywordfunction:
function functionname ()
{
Here is the code to run
}
What happens when a function is called ? you should know how to call it. !
Ability to call functions directly when an event occurs ( for example, when a user taps a button ), and can be called by JS at any location .
Hint : JS is sensitive to uppercase and lowercase ?
so function keyword how to write ?
Calling a function with a number of parameters
When you call a function , You can think of the pass-through value , which becomes the number of 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 it as a variable :
function MyFunction (VAR1,VAR2)
{
The code to run
}
Variables and parameters must appear in a suppressed order . The first variable is the given value of the first passed parameter , etc. .
Example :
<script>
Function Method (Name,age)
{
Alert ("Name:" +name+ ", Age:" +age);
}
Method ("Syx", 22);
</script>
In fact, this simple invocation doesn't make any sense , does it? mainly to have a kind of cattle X the effect , ability to trigger a function from 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 running and returns the specified value .
Grammar
function Method ()
{
var x=5;
return x;
}
The function above will return 5.
Gaze : the whole JS does not stop running , but functions . JS will continue to run the code from where the function was 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 number of parameters passed to the function :
<p> The function called by this example runs a calculation. And then return 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 only want to exit the function . The return value is optional :
function MyFunction (A, B)
{
if (a>b)
{
return;
}
X=a+b
}
Assuming a>b, the above code exits , and the value of a+b is not computed .
Local JS variable
variables declared inside the Js function ( using var) are local variables , so you can only access them inside the function . . ( the scope of the variable is local ).
You can name the same local variable in a different function , because only the function that declares the variable has the ability to recognize the variable .
the local variable is deleted only if the function execution is complete .
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 begins at the time they are declared .
Local variables are deleted after the function is executed .
Global variables are deleted after the page is closed .
Assigning a value to an undeclared JS variable
Suppose you assign a value to a variable that has not yet been declared , and the variable will be declared as a global variable by its own initiative . Case :
Value= "Hello,world";
Will life a global variable value. in time he runs inside the function .
JS Learning Day----function