' JS ' Custom function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.