Functions and arrays of JavaScript basics

Source: Internet
Author: User

function

Basic concepts of functions

A collection of Program directives (statements) that complete a function, called a function. Some programmers refer to the function as a method, I hope you do not get dizzy by these two nouns.

Functions are divided into: custom functions, system functions (often see JS help manual).

JS Custom Function Basic syntax:

function function name (parameter list) {    // code statement    return value;   There are no return values that can be selected. }
    1. Parameter list: Represents the input of a function

    2. Function Body: Represents a block of code in order to implement a function
    3. function can have a return value, or it can have no

Cases

function. HTML:

<! DOCTYPE html><script type= "Text/javascript" src= "my function. js" ></script><script type= "Text/javascript" ><!--for browser compatibility, sometimes this is written            var num1 = window.prompt ("Please enter first number");            var num2 = window.prompt ("Please enter a second number");            var operator = window.prompt ("Please enter Operator");            NUM1 = parsefloat (NUM1);            num2 = parsefloat (num2);            //How to call a function            Document.writeln ("res=" +jisuan (NUM1, num2, operator)); //  -</script>

My function. js:

//Custom FunctionsfunctionJisuan (NUM1, num2, operator) {//Special emphasis is placed on the name of the parameter without var    varres = 0; if(operator = = "+") {res= Num1 +num2; } Else if(operator = = "-") {res= NUM1-num2; } Else if(operator = = "*") {res= NUM1 *num2; } Else{res= NUM1/num2; }    returnRes//return}

If more than one html/php/jsp is to use a custom function, it must be introduced. The above function is presented separately, written to the JS file, and then introduced in the desired html/php/jsp place.

JS File Introduction Syntax:

<script language= "javascript" src= "JS path" ></script> or <script type= "Text/javascript" src= "JS path" > </script>

Several global functions are described below:

The eval () function computes a string and executes the JavaScript code in it. Such as:

var str = "Window.alert (' OK ')"; // Requirement: Perform eval (str) as a script for Str ;

The escape () function encodes the string so that it can be read on all computers.

The unescape () function decodes a string encoded by escape ().

Escape and unescape functions are often used together to prevent garbled occurrences. Such as:

Use the Escape () function to encode the string "Li Ahi" first:

var str1 = "Li Ahi"; var str2 = Escape (str1); Window.alert (str2);

At this time the browser popup garbled:

The unescape () function is then used to decode the string encoded by Escape ():

var str3 = unescape (str2); Window.alert (STR3);

This shows normal:

several ways to call functions

    1. Function name (passed to the function parameter 1, parameter 2, ...)
    2. variable = function name, when the variable is equivalent to the function reference (pointer), you can call the function: variable (actual argument ...)

Cases

My function. js:

function Test (val) {    Window.alert ("you entered is" +val);}

function. HTML (partial):

Test ("Hello, World");

The popup dialog box displays "Hello, World".

Window.alert (test);

The following dialog box pops up (the entire function is displayed):

var myvar =  test; Window.alert (MyVar); MyVar (  "Beijing, China");

First, the entire Function dialog box pops up, and then the string "Beijing, China" pops up.

Special attention:

    1. For function calls that have return values, you can also use the returned results directly in your program. (That is, if there is a return value, what is it?)

    2. a function that does not return a value returns underined. (for example, if the test () function does not return a value, but you accept it, the return is undefined).

function Call--recursion

The functions are as follows:

function ABC (NUM1) {    if(Num1 > 3) {        ABC (--NUM1);   recursive     }    Document.writeln (NUM1);}

Function call:

ABC (5);

Output results: 3 3 4.

Recursive analysis

functions--in-depth use

    1. The argument list for a function can be multiple
    2. The argument list can be multiple, and the data type can be any type
    3. JS supports functions with variable number of parameters
    4. JS supports creating dynamic functions

example, write a function that can accept any number of numbers and calculate their and.

My function. js:

function ABC2 (N1) {// parameter How much does not care about the nature, only concern function name    // in JS there is a arguments, Access to all incoming values    //Window.alert (arguments.length);     for (var i = 0; i < arguments.length; i++) {        window.alert (arguments[i]);}    }

function. HTML (partial):

Window.alert ("ABC2 (45, 90, 900);"); ABC2 (45, 90, 900);//Can Quan Zhuan numeric Window.alert ("ABC2 (4,\" Hello, world\ "); ABC2 (4, "Hello, World");//Can be passed the value of the string Window.alert ("ABC2 ();"); ABC2 ();//Can not preach anything

Create a function using the functions class (???)

To create a basic syntax format for dynamic functions:

var varname=new function (argument1,..., lastargument);

Note: All parameters must be string type, and the final parameter must be the function code of the dynamic function.

Cases

var square=Newfunction("x", "Y", "var sum;sum=x*x+y*y;return sum;") ); alert (Square (3,2)); var alsodosquare=Doadd;alert (Alsodosquare (3,2));

Note: The scope and this of closures and functions are not yet spoken, and must be explained in object-oriented.

Functions and arrays of JavaScript basics

Related Article

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.