-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.html
71 lines (52 loc) · 1.76 KB
/
functions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo Functions</title>
</head>
<body>
<script type="text/javascript">
"use strict";
function hello(somebody) {
return 'Hello ' + somebody;
}
// Parameter may be of any type
document.write( '<br/>--- Parameter may be of any type ---<br/>' );
document.write( hello('Somebody') + '<br/>');
document.write( hello(2) + '<br/>' );
document.write( hello(null) + '<br/>' );
document.write( hello() + '<br/>' );
document.write( '<br/>' );
// Parameter counts may exceed defined
document.write( '<br/>--- Exceed number of parameters ---<br/>' );
document.write( hello('Somebody', 23) + '<br/>' );
// Anonymous function
document.write( '<br/>--- Function Literals ---<br/>' );
var func = function() {
return 'Anonymous function';
}
document.write( func() + '<br/>' );
// dynamic list of parameters
document.write( '<br/>--- Dynamic list of parameters ---<br/>' );
func = function() {
console.log('Function called with ' + arguments.length + ' arguments');
for(var key in arguments) {
document.write( key + ' -> ' + arguments[key] + '<br/>' );
};
}
func();
func(1);
func('Somebody', 123);
// Function without return
document.write( '<br/>--- Function without return ---<br/>' );
document.write( func() + '<br/>' );
// Nested Functions
document.write( '<br/>--- Nested Functions ---<br/>' );
function hypotenuse(a, b) {
function square(x) { return x*x; }
return Math.sqrt(square(a) + square(b));
}
document.write( hypotenuse(3, 5) + '<br/>' );
</script>
</body>
<html>