JavaScript learns the JS function
A function is a block of code wrapped in curly braces, preceded by a keyword function:
function functionname () { Here is the code to execute}
function Parameters
The parameters of a function can be arbitrarily multiple, without declaring the variable type , only by giving the variable name:
function myFunction (name, job) { This is the code to execute}
function return value
Using the return statement in the function, the function stops executing and returns where it was called.
The return value of the function does not have to be declared type, and can be returned directly.
function myFunction () { var x=5; return x;}
The function above returns a return value of 5.
Note: The entire JavaScript does not stop executing, just the function.
JavaScript will continue to execute code from where the function is called.
The function call will be replaced by the return value:
var myvar=myfunction ();
You can also use the return statement only if you want to exit the function.
The return value is optional:
function MyFunction (b) { if (a>b) { return;} x=a+b;}
When a is greater than B, it does not go down, but returns directly.
Local Variables
About local variables and global variables say it again here.
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 the variable is local).
You can use a local variable with the same name in a different function, because only a function that declares the variable will recognize it.
As soon as the function is complete, the local variable is deleted.
Global variables
Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.
Note: Assign a value to an undeclared JavaScript variable:
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
This statement:
Carname= "Volvo";
A global variable is declared carname, even if it executes within the function.
function Instance
<Body><ScriptType= "Text/javascript">functionMember (name, job)//Analogy to Java's constructor, JS is no class concept{This. Name=NameThis. Job=Job }functionShowproperty (obj, objstring) {VarStr="";For(VarIInchobj) {//Iterate through every property in an objectStr+=Objstring+"."+I+"="+Obj[i]+"<br/>";//I represents a property//Obj[i] Represents the value of the property}ReturnStr }VarObj= new Member ( " Andy Lau " artist ) // Document.writeln (Showproperty (obj, "person ); </script></body>
Output:
Person.name= Andy Lau person.job= artist
References
Santhiya Garden Zhang Long teacher Java web video tutorial.
W3school JavaScript Tutorial: http://www.w3school.com.cn/js/index.asp
English version: http://www.w3schools.com/js/default.asp
JavaScript learns the JS function