The function is a code block wrapped in curly braces, and the keyword function is used before:
The code copy is as follows:
function functionName()
{
Here is the code to be executed
}
Function parameters
The function can have as many parameters as possible, without declaring the variable type, just giving the variable name:
The code copy is as follows:
function myFunction(name, job)
{
Here is the code to be executed
}
Function returns value
Using the return statement in the function, the function stops execution and returns to where it is called.
The return value of the function does not need to declare the type, just return it directly.
The code copy is as follows:
function myFunction()
{
var x=5;
return x;
}
The above function will return the return value of 5.
Note: The entire JavaScript will not stop executing, it is just a function.
JavaScript will continue to execute the code from where the function is called.
The function call will be replaced by the return value:
The code copy is as follows:
var myVar=myFunction();
You can also use the return statement when you just want to exit the function.
The return value is optional:
The code copy is as follows:
function myFunction(a,b)
{
if (a>b)
{
return;
}
x=a+b;
}
When a is greater than b, it will not be executed downwards, but will be returned directly.
Local variables
Let’s repeat this about local variables and global variables.
A variable declared inside a JavaScript function (using var) is a local variable, so it can only be accessed inside the function. (The scope of this variable is local).
Local variables with the same name can be used in different functions, because only functions that have declared the variable can be recognized.
As long as the function is run, the local variable will be deleted.
Global variables
The variable declared outside the function is a global variable, which can be accessed by all scripts and functions on the web page.
Note: Assign values to undeclared JavaScript variables:
If you assign a value to a variable that has not been declared, the variable will be automatically declared as a global variable.
This statement:
carname="Volvo";
A global variable carname will be declared even if it is executed within the function.
Function example
The code copy is as follows:
<body>
<script type="text/javascript">
function member(name, job) //Analogizes Java constructor, JS has no concept of class
{
this.name = name;
this.job = job;
}
function showProperty(obj, objString) {
var str = "";
for ( var i in obj) {
//Transfer every property in the object
str += objString + "." + i + "=" + obj[i] + "<br/>";
//i means attribute
//obj[i] represents the value of this property
}
return str;
}
var obj = new member("Andy Lau", "Artist");//Create an object instance
document.writeln(showProperty(obj, "person"));
</script>
</body>
Output:
The code copy is as follows:
person.name=Andy Lau
person.job=Artist
The above is the entire content of this article. I hope you can like it. If you have any questions, please leave me a message.