Home >Web Front-end >JS Tutorial >What is a Javascript pseudo-array? A brief introduction to javascript pseudo arrays

What is a Javascript pseudo-array? A brief introduction to javascript pseudo arrays

不言
不言Original
2018-09-27 17:34:422549browse

The content of this article is about what is a Javascript pseudo-array? A brief introduction to JavaScript pseudo-arrays has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is a pseudo array?

Pseudo array is a json object containing the length attribute

For example:

{
    0: 1,
    1: 2,
    length: 2
}

Common pseudo array

arguments, NodeList, HTMLCollection, Jquery object...

How to convert pseudo data into a standard array

Use Array.slice

function toArray() {
    console.log(arguments instanceof Array) // false
    arguments = Array.prototype.slice.call(arguments)
    console.log(arguments instanceof Array) // true
    return arguments
}
toArray(1,2,3) // [1, 2, 3]

Array.slice source code analysis

function ArraySlice(start, end) {
    CHECK_OBJECT_COERCIBLE(this, "Array.prototype.slice");

    var array = TO_OBJECT(this); 
    var len = TO_LENGTH(array.length); // 取数据length
    var start_i = TO_INTEGER(start); // 开始值转Number
    var end_i = len; // 结束值直接取array的length

    if (!IS_UNDEFINED(end)) end_i = TO_INTEGER(end); // 参数有end则使用end

    if (start_i < 0) { // 开始值为负数,重新计算值,从尾部往前推算
        start_i += len;
        if (start_i < 0) start_i = 0; // 负数的绝对值超过长度,开始值赋值为0
    } else {
        if (start_i > len) start_i = len; // 开始值超过长度, 开始值赋值为len
    }

    if (end_i < 0) { // 结束值为负数,重新计算值,从尾部往前推算
        end_i += len;
        if (end_i < 0) end_i = 0; // 负数的绝对值超过长度,结束值赋值为0
    } else {
        if (end_i > len) end_i = len; // 开始值超过长度, 结束值赋值为len
    }

    var result = ArraySpeciesCreate(array, MaxSimple(end_i - start_i, 0)); // 创建一个数组

    if (end_i < start_i) return result; // 结束值小于开始值,那么直接返回空数组

    if (UseSparseVariant(array, len, IS_ARRAY(array), end_i - start_i)) { // array是数组
        %NormalizeElements(array);
        if (IS_ARRAY(result)) %NormalizeElements(result);
        SparseSlice(array, start_i, end_i - start_i, len, result);
    } else { // array不是数组
        SimpleSlice(array, start_i, end_i - start_i, len, result);
    }

    result.length = end_i - start_i;  // 数组长度赋值

    return result;
}
/*
* array 具体操作的数组
* start_i 开始位置
* del_count 需要处理的长度
* len 数组长度
* deleted_elements 利用浅拷贝,返回结果,对于slice来说,是选择的那部分数组,对于splice来说,是删除的那些数组
*/
function SparseSlice(array, start_i, del_count, len, deleted_elements) {
    // Move deleted elements to a new array (the return value from splice).
    var indices = %GetArrayKeys(array, start_i + del_count);
    if (IS_NUMBER(indices)) {
        var limit = indices;
        for (var i = start_i; i < limit; ++i) {
            var current = array[i];
            if (!IS_UNDEFINED(current) || i in array) {
                %CreateDataProperty(deleted_elements, i - start_i, current);
            }
        }
    } else {
        var length = indices.length;
        for (var k = 0; k < length; ++k) {
            var key = indices[k];
            if (key >= start_i) {
                var current = array[key];
                if (!IS_UNDEFINED(current) || key in array) {
                    %CreateDataProperty(deleted_elements, key - start_i, current);
                }
            }
        }
    }
}
/*
* array 具体操作的数组
* start_i 开始位置
* del_count 需要处理的长度
* len 数组长度
* deleted_elements 利用浅拷贝,返回结果,对于slice来说,是选择的那部分数组,对于splice来说,是删除的那些数组
*/
function SimpleSlice(array, start_i, del_count, len, deleted_elements) {
    for (var i = 0; i < del_count; i++) {
        var index = start_i + i;
        if (index in array) {
            var current = array[index];
            %CreateDataProperty(deleted_elements, i, current);
        }
    }
}

The above is the detailed content of What is a Javascript pseudo-array? A brief introduction to javascript pseudo arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn