Learning Goals:
1. Fully understand the concept of functions
2. Proficiency in Custom functions
3. Mastering The common system functions in JS
A simple subtraction case (explaining the necessity of the function):
<HTML><Head><Scriptlanguage= "JavaScript"><!--//enter two numbers, and then enter an operator (+-*/) to get the resultvarNUM1=Window.prompt ("Please enter a 1th num");varnum2=Window.prompt ("Please enter a 2nd num");varoperator=Window.prompt ("Please enter operator"); Num1=parsefloat (NUM1); num2=parsefloat (num2);varRes= 0;if(operator=="+") {res=NUM1+num2;}Else if(operator=="-") {res=NUM1-num2;}Else if(operator=="*") {res=NUM1*num2;}Else{res=NUM1/num2;}document.write ("res="+res);// -</Script></Head><Body></Body><HTML>
The concept of a function
Js function
Why do ① need functions
For example, in multiple HTML files need to complete (+-*/ and other functions)
Write the code into a function,
Basic concepts of ② functions
A collection of code (statements, directives) to complete a function
③ Basic Syntax
function name (argument list){
Code
return value;// optional
}
④ case : put the function separately, write to the JS file and introduce it where needed
<script language= "javascript" src = " path " ></script>
Js24-2.html Code:
<HTML><Head></Head><Body><Scriptlanguage= "JavaScript"src= "Js24-2.js"></Script><Scriptlanguage= "JavaScript">//enter two numbers, and then enter an operator (+-*/) to get the resultvarNUM1=Window.prompt ("Please enter a 1th num");varnum2=Window.prompt ("Please enter a 2nd num");varoperator=Window.prompt ("Please enter operator"); Num1=parsefloat (NUM1); num2=parsefloat (num2);//calling Functionsdocument.write ("Res"+Jisuan (NUM1, num2, operator));</Script></Body><HTML>
Js24-2.js
//js file//Custom Functions//enter two numbers, and then enter an operator (+-*/) to get the resultfunctionJisuan (NUM1, num2, operator) {//Special emphasis is placed on the name of the parameter without varvarres = 0;if(operator== "+") {res=num1+num2;}Else if(operator== "-") {res=num1-num2;}Else if(operator== "*") {res=num1*num2;}Else{res=num1/num2;}returnRes;}
' JS ' Custom function