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. 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值