JavaScript Functions

Functions are the core of any language, because they allow the encapsulation of statements that can be run anywhere and at any time. Functions in ECMAScript are declared using the function keywork, followed by a set of arguments and then the body of the function. The basic syntax is as follows:

 

function functionName(arg0, arg1, ... , argN) {
    statements
}

 

 

Here are some examples.

Example01

// Function Example 1
function sayHi(name, message) {
	alert("Hello " + name + ", " + message);
}
sayHi("JavaScript", "how are you today?");
 

 

Example02

// Function Example 2
function sum(num1, num2) {
	return num1 + num2;
}
var result = sum(5, 10);
alert(result);

 

 

Example03

// Function Example 3
function diff(num1, num2) {
	if (num1 < num2) {
		return num2 - num1;
	} else {
		return num1 - num2;
	}
}

var result = diff(7, 10);
alert(result);

 

 

Example04

// Function Example 4
function sayHi(name, message) {
	return;
	alert("Hello " + name + ", " + message); //never called
}

sayHi("JavaScript", "how are you today?");

 

 

Example05

// Function Example 5
function sayHi() {
	alert("Hello " + arguments[0] + ", " + arguments[1]);
}

sayHi("JavaScript", "how are you today?");

 

 

Example06

// Function Example 6
function howManyArgs() {
	alert(arguments.length);
}

howManyArgs("string", 45); //2
howManyArgs(); //0
howManyArgs(12); //1

 

 

Example07

// Function Example 7
function doAdd() {
	if (arguments.length == 1) {
		alert(arguments[0] + 10);
	} else if (arguments.length == 2) {
		alert(arguments[0] + arguments[1]);
	}
}

doAdd(10); //20
doAdd(30, 20); //50

 

Example08

// Function Example 8
function doAdd(num1, num2) {
	if (arguments.length == 1) {
		alert(num1 + 10);
	} else if (arguments.length == 2) {
		alert(arguments[0] + num2);
	}
}

doAdd(10); //20
doAdd(30, 20); //50

 

Example09

// Function Example 9
function doAdd(num1, num2) {
	arguments[1] = 10;
	alert(arguments[0] + num2);
}

doAdd(10, 20); //20
doAdd(30, 20); //50

This version of doAdd() always overwrites the second argument with a value of 10. Because values in the arguments object are automatically reflected by the corresponding named arguments, the change to arguments[1] also changes the value of num2, so both have a value of 10. This doesn't mean that both access the same memory space, though; their memory spaces are separate but happen to be kept in sync. This effect goes only one way: changing the named argument does not result in a change to the corresponding value in arguments. Another thing to keep in mind: if only one argument is passed in, then setting arguments[1] to a value will not be reflected by the named argument. This is because the length of the arguments object is set based on the number of arguments passed in, not the number of named arguments listed for the function.

All arguments in ECMAScript are passed by value. It is not possible to pass arguments by reference.

 

Example10

// Function Example 10
function addSomeNumber(num) {
	return num + 100;
}

function addSomeNumber(num) {
	return num + 200;
}

var result = addSomeNumber(100); //300
alert(result);

ECMAScript functions cannot be overloaded in the traditional sense. If two functions are defined to have the same name in ECMAScript, it is the last function that becomes the owner of that name. 

 

你可能感兴趣的:(JavaScript)