How JavaScript defines a function

Source: Internet
Author: User

How JS defines a function

Method 1:

Function name (parameter) {

function Body}

function Singlefun (a) {
Alert ("I am a parameter of the way one defines a function passed in with a value of:" + a);
}

Singlefun (666);

Method 2:var Function name =new function ("parameter", "function")

var sum = new Function ("Num1,num2", "return num1+num2");
Function call
var result1 = sum (120,130,66);
alert (RESULT1);

Method 3:var Function name =function (parameter) {List of functions}

var funname = function (A, b) {
alert (a); Abc
alert (b); True
}
Function call
Funname ("abc", True);

Arguments can be used directly in the body of a function, and we do not need to define it; he can accept all parameter lists

function sum () {

var result = 0;

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

var p = arguments[i];

Alert (typeof (P) + "value" +p);

if (typeof (P) = = "Number") {
result + = P;
}
}
return result;
}
var result = SUM (12,88, "100", 50);
Alert ("And as:" +result)

How JavaScript defines a function

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.