The function in JS

Source: Internet
Author: User

First, the way the function is defined

(1) Normal way

Syntax: function name (parameter) {

function body

}

Code:

Function method () {

Alert ("TestMethod");

}

Method ();

(2) anonymous function

Syntax: function (parameter) {

function body

}

Code:

var method = function () {

Alert ("yyy");

};

Method ();

(3) Object functions

Syntax: new function (argument list, function body);

Note: The parameter name must be in the form of a string, and the last one is the function body and the function body needs to be in string form.

Code:

var fn = new Function ("A", "B", "Alert (a+b)");

FN (2,5);

Second, the parameters of the function

Note: (1) parameter does not have VAR modifier

(2) The format of formal parameters and arguments is not necessarily equal

(3)arguments objects are arrays that encapsulate the passed arguments

Code:

function fn (a,b,c) {

var sum = a+b+c;

alert (sum);

Arguments is the number of arrays that encapsulate the passed arguments

for (Var i=0;i<arguments.length;i++) {

Alert (Arguments[i]);

}

}

FN (1,2,4,8);

Third, the return value of the function

(1) You do not have to indicate whether you have a return value when defining a function

(2) The return value simply passes the return keyword and the return code does not execute

Code:

function fn (A, b) {

return a+b;

Alert ("XXXX");// The code here does not execute

}

Alert (FN (2,3));

Four, JS global function

Global properties and functions are available for all built-in JavaScript objects

(1) encoding and decoding

decodeURI () decodes a coded URI.

decodeURIComponent () decodes a coded URI component.

encodeURI () encodes the string as a URI.

encodeURIComponent () encodes the string as a URI component.

Escape () encodes a string

Code:

var url = "Http://www.baidu.com?name= Zhang &password=12//3";

Alert (encodeURI (URL));

Http://www.baidu.com?name=%E5%BC%A0&password=123

Alert (encodeuricomponent (URL));

Http%3a%2f%2fwww.baidu.com%3fname%3dzhangsan%26password%3d123

Alert (Escape (URL));

Http%3a//www.baidu.com%3fname%3dzhangsan%26password%3d123

(2) Forced conversion

Number () Converts the value of the object to a digit.

String () Converts the value of the object to a string.

(3) Conversion to Digital

parseint () parses a string and returns an integer.

Parsefloat () parses a string and returns a floating-point number.

(4) eval () computes the JavaScript string and executes it as script code.

Code:

var str = "var a=2;var b=3;alert (a+b)";

eval (str);

function Print (str) {

eval (str);

}

Print (" custom Logic ");

 

The function in JS

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.