arguments 类数组
生活随笔
收集整理的這篇文章主要介紹了
arguments 类数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、類數組:長得像數組,可以拿它當數組用,但它不是數組
- 可以利用屬性名模擬數組的特性
- 可以動態的增長 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 指向這個函數的調用者
總結
以上是生活随笔為你收集整理的arguments 类数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows消息和事件的区别 VC++
- 下一篇: 位掩码(bitmask)在windows