一、类数组:长得像数组,可以拿它当数组用,但它不是数组
- 可以利用属性名模拟数组的特性
- 可以动态的增长
length
的属性 - 如果强行让类数组调用
push
方法,则会根据length
属性值的位置进行属性的扩充
二、不能往类数组里面添加东西
function test() {console.log(arguments);arguments.push('age'); // arguments.push is not a function
}test(1, 2, 3);
三、类数组的组成
- 属性要为索引(数字)
- 必须有
length
属性 - 最好加上
push
类数组的好处就是把对象和数组的方法,全拼在一起
var obj = {'0': 'a','1': 'b','2': 'c','length': 3,'push': Array.prototype.push,'splice': Array.prototype.splice
};Array.prototype.push = function(target) {obj[obj.length] = target;obj.length++;
}obj.push('d');
console.log(obj);
四、 arguments.callee
指向函数自身的引用
function test() {console.log(arguments.callee); // function test() {...}function demo() {console.log(arguments.callee); // function demo() {...}}demo();
}
test();
递归求阶乘
var num = (function (n) {if (n === 1) {return 1;}return n * arguments.callee(n - 1);
} (5))
五、arguments.callee.caller
指向这个函数的调用者