js类数组对象与arguments

简单来说,和数组类似,拥有length属性,可以通过索引来访问或设置里面的元素,但是不能使用数组的方法。

arr[0]

这里的arr一定是一个数组吗?不一定,也可能是一个对象。

let arr = {
    0: 'dazhi'
}
console.log(arr[0]); // dazhi

注意,这边arrLike必须加上length属性,不然它就是一个普通对象而已
为什么叫做类数组对象呢, 我们从读写,获取长度,遍历这三个方面来看看这两个对象

let arr = ['name', 'age', 'job'];

        // 创建一个类数组对象
        // 这边arrLike必须加上length属性,不然它就是一个普通对象而已。
        let arrLike = {
            0: 'name',
            1: 'age',
            2: 'job',
            length: 3
        }

        // 读
        console.log(arr[0]); // name
        console.log(arrLike[0]); // name

        // 写
        arr[0] = 'new name';
        arrLike[0] = 'new name';

        console.log(arr[0]); // new name
        console.log(arrLike[0]); // new name

        // 获取长度
        console.log(arr.length); // 3
        console.log(arrLike.length); // 3

        // 遍历
        for (let i = 0;i < arr.length; i++){
            console.log(arr[i]);
        }
        
        // name 
        // age 
        // job
        
        for (let i = 0;i < arrLike.length; i++){
            console.log(arrLike[i]);
        }
        // name 
        // age 
        // job

        // 如果用数组的方法
        arr.push('gender'); 
        // arrLike.push('gender');  // 报错 Uncaught TypeError: arrLike.push is not a function,  需要转换为数组
转换为数组的方法

1. slice

// slice 返回一个新数组
        console.log(Array.prototype.slice.call(arrLike)); // ["name", "age", "job"]

2. splice

// splice 
        console.log(Array.prototype.splice.call(arrLike,0)); // ["name", "age", "job"]

3. ES6 Array.from

// from 
        console.log(Array.from(arrLike));

4. apply

// apply
        console.log(Array.prototype.concat.apply([],arrLike));

Arguments 对象就是一个类数组对象。 在客户端JavaScript中,一些DOM方法(document.getElementsByTagName()等)也返回类数组对象。

Arguments对象

Arguments 对象只定义在函数体重,包括了函数的参数和其他属性。 在函数体重,arguments 指该函数的Arguments 对象。

function foo(name, age, sex) {
    console.log(arguments);
}

foo('name', 'age', 'sex')
image.png

length属性
Arguments对象的length属性,表示实参的长度

function foo(b, c, d){
            console.log("实参的长度为:" + arguments.length)
        }
        
        console.log("形参的长度为:" + foo.length)
        
        foo(1)
        
        // 形参的长度为:3
        // 实参的长度为:1

callee属性
Arguments 对象的callee属性,通过它可以调用函数自身。

arguments和对应参数的绑定

function foo(a, b, c, d) {
    // "use strict";
    console.log(a, arguments[0]); // 1 1
    
    // 改变形参
    a = 11;
    
    console.log(a, arguments[0]); // 11 11
    
    // 改变arguments
    arguments[1] = 22;
    
    console.log(b, arguments[1]); // 22 22
    
    // 未传入的参数
    console.log(c); // undefined
    
    c = 3;
    
    console.log(c, arguments[2]); // 3 undefined
    
    arguments[3] = 4;
    console.log(d, arguments[3]); // undefined 4
}

foo(1, 2);

传递参数
将参数从一个函数传递到另一个函数

function foo() {
            bar.apply(this, arguments);
        }
        
        function bar(a, b, c) {
            console.log(a, b, c);
        }
        
        foo(1, 2, 3);
        
        // 1 2 3

arguments转为数组
arguments 除了可以用上面几个转为数组的方法,还可以使用ES6的...展开符

function bar(a, b) {
  // 用...把argumens转为数组
  console.log([...arguments]); // [1, 2]
}

bar(1, 2);

面试题

var obj = {
    '2': 3,
    '3': 4,
    'length': 2,
    'splice': Array.prototype.splice,
    'push': Array.prototype.push
}
obj.push(1)
obj.push(2)
console.log(obj)
  • push方法
    push方法根据length属性来决定从哪里开始插入给定的值。
    如果length不能被转成一个数值,则插入的元素索引为0,包括length不存在时。
    当length不存在时,将会创建它。

分析:
obj类数组的length为2, obj.push(1)则从obj[2]开始插入, 而obj对key(2)赋值了3,此时会替换key(2)的值, length也对应成了3
obj.push(2), 此时的类数组length为3,则是从obj[3]开始插入,就替换了key(3)的值,输出结果为


image.png

如果obj 初始化length为0,则输出结果不一样

var obj = {
            '2': 3,
            '3': 4,
            'length': 0,
            'splice': Array.prototype.splice,
            'push': Array.prototype.push
        }
        // for(var i = 0, len = obj.length; i < len; i++) {
        //     // 根据obj.length的长度循环出几条数据 
        //     console.log(obj[i]); // undefined undefined   
        // }
        // 'length': 5,
        console.log(obj[0],obj[1],obj[2],obj[3],obj.length); // undefined undefined 3 4 5

        obj.push(1)
        console.log(obj[0],obj[1],obj[2],obj[3],obj.length);
        obj.push(2)
        console.log(obj[0],obj[1],obj[2],obj[3],obj.length); 
        console.log(obj);
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容