js0008

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

<!DOCTYPE html>
<html>
<head>
<title>Using the arguments parameter</title>
<meta charset="utf-8">
<script src="../assert.js"></script>
<link rel="stylesheet" type="text/css" href="../assert.css">
</head>
<body>
<script>
function whatever(a, b, c){
assert(a === 1, 'The value of a is 1');
assert(b === 2, 'The value of b is 2');
assert(c === 3, 'The value of c is 3');

assert(arguments.length === 5,
'We’ve passed in 5 parameters');

assert(arguments[0] === a,
'The first argument is assigned to a');
assert(arguments[1] === b,
'The second argument is assigned to b');
assert(arguments[2] === c,
'The third argument is assigned to c');

assert(arguments[3] === 4,
'We can access the fourth argument');
assert(arguments[4] === 5,
'We can access the fifth argument');
}

whatever(1,2,3,4,5);
</script>
</body>
</html>

js函数 隐式的函数参数arguments this

这两者会静默地传递給函数

arguments 参数表示函数调用过程中传递的所有参数

this 表示被调用函数的上下文对象

arguments对象 不是数组 !!!

是一个类数组的结构

可以作为函数参数的别名 但是在严格模式下无法使用

四种方式调用函数

  • 作为函数直接调用
  • 关联在一个对象上 实现面向对象编程oop 方法调用
  • 作为一个构造函数
  • 通过函数的apply 或者是 call方法