1. 类数组对象
所谓的类数组对象: 拥有⼀个 length 属性和若⼲索引属性的对象
举个例⼦:
var array = ['name', 'age', 'sex'];
var arrayLike = {
0: 'name',
1: 'age',
2: 'sex',
length: 3
}
读写
console.log(array[0]); // name
console.log(arrayLike[0]); // name
array[0] = 'new name';
arrayLike[0] = 'new name';
⻓度
console.log(array.length); // 3
console.log(arrayLike.length); // 3
遍历
for(var i = 0, i < array.length; i++) {
……
}
for(var i = 0, i < arrayLike.length; i++) {
……
}
但是调⽤原⽣的数组⽅法会报错,如push
arrayLike.push is not a function
调⽤数组⽅法
只能通过 Function.call 间接调⽤
var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 }
Array.prototype.join.call(arrayLike, '&'); // name&age&sex
Array.prototype.slice.call(arrayLike, 0); // ["name", "age", "sex"]
// slice可以做到类数组转数组
Array.prototype.map.call(arrayLike, function(item){
return item.toUpperCase();
});
// ["NAME", "AGE", "SEX"]
类数组转数组
var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 }
// 1. slice
Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"]
// 2. splice
Array.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"]
// 3. ES6 Array.from
Array.from(arrayLike); // ["name", "age", "sex"]
// 4. apply
Array.prototype.concat.apply([], arrayLike)
2. Arguments对象
Arguments 对象只定义在函数体中,包括了函数的参数和其他属性。在函数体中,arguments 指代该函 数的 Arguments 对象。
举个例⼦:
function foo(name, age, sex) {
console.log(arguments);
}
foo('name', 'age', 'sex')
打印结果:
可以看到除了类数组的索引属性和length属性之外,还有⼀个callee属性
length属性
Arguments对象的length
属性,表示实参的⻓度,举个例⼦:
function foo(b, c, d){
console.log("实参的⻓度为:" + arguments.length)
}
console.log("形参的⻓度为:" + foo.length)
foo(1)
// 形参的⻓度为:3
// 实参的⻓度为:1
callee属性
arguments.callee
是一个指向当前正在执行的函数的引用,它允许函数在内部引用自身。
在早期版本的 JavaScript 中,arguments.callee
被广泛用于递归调用和匿名函数的自引用。然而,由于 ECMAScript 5 的严格模式(strict mode)禁止了使用 arguments.callee
,因此现在不再推荐使用它,而应该使用函数的命名引用。
以下是关于 arguments.callee
的一些重要点:
- 引用当前函数:
arguments.callee
是一个指向当前正在执行的函数的引用,即使函数是匿名的也可以使用。 - 递归调用: 通过
arguments.callee
可以在函数内部实现递归调用,而不必关心函数的名称。例如:
var factorial = function(n) {
if (n <= 1) {
return 1;
} else {
return n * arguments.callee(n - 1);
}
};
- 匿名函数的自引用: 对于匿名函数,如果需要在函数内部引用自身,
arguments.callee
是一个有效的选择。例如:
var fib = function(n) {
if (n <= 1) {
return n;
} else {
return arguments.callee(n - 1) + arguments.callee(n - 2);
}
};
虽然 arguments.callee
可以解决一些问题,但它也存在一些问题和限制:
- 性能影响: 在一些 JavaScript 引擎中,访问
arguments.callee
可能会导致性能下降,因为它会使函数变得更难优化。 - 严格模式下禁止使用: 在严格模式下,访问
arguments.callee
会导致错误。 - 可读性差: 使用
arguments.callee
可能会使代码变得更难理解和维护,因为它隐藏了函数的真实名称。
因此,在现代 JavaScript 中,除非特别需要,一般推荐使用函数的命名引用而不是 arguments.callee
。
讲个闭包经典⾯试题使⽤ callee 的解决⽅法:
var data = [];
for (var i = 0; i < 3; i++) {
(data[i] = function () {
console.log(arguments.callee.i)
}).i = i;
}
data[0]();
data[1]();
data[2]();
// 0
// 1
// 2
arguments 和对应参数的绑定
function foo(name, age, sex, hobbit) {
console.log(name, arguments[0]); // name name
// 改变形参
name = 'new name';
console.log(name, arguments[0]); // new name new name
// 改变arguments
arguments[1] = 'new age';
console.log(age, arguments[1]); // new age new age
// 测试未传⼊的是否会绑定
console.log(sex); // undefined
sex = 'new sex';
console.log(sex, arguments[2]); // new sex undefined
arguments[3] = 'new hobbit';
console.log(hobbit, arguments[3]); // undefined new hobbit
}
foo('name', 'age')
传⼊的参数,实参和 arguments 的值会共享,当没有传⼊时,实参与 arguments 值不会共享
传递参数
将参数从⼀个函数传递到另⼀个函数
// 使⽤ apply 将 foo 的参数传递给 bar
function foo() {
bar.apply(this, arguments);
}
function bar(a, b, c) {
console.log(a, b, c);
}
foo(1, 2, 3)
ES6
使⽤ES6的 ... 运算符,我们可以轻松转成数组。
function func(...arguments) {
console.log(arguments); // [1, 2, 3]
}
func(1, 2, 3);
应⽤
arguments的应⽤其实很多,像参数不定⻓、函数柯⾥化等等都有涉及。