Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现一个原生的数组push #4

Open
lynhao opened this issue Sep 12, 2020 · 0 comments
Open

实现一个原生的数组push #4

lynhao opened this issue Sep 12, 2020 · 0 comments

Comments

@lynhao
Copy link
Owner

lynhao commented Sep 12, 2020

屏幕快照 2020-09-12 23 05 49

Array.prototype._push = function(...items) {
    let O = new Object(this);
    let len = O.length >>> 0;  // 取整
    let argCount = items.length >>> 0; // 取整
    let E = O;
    if (len + argCount > 2 ** 53 - 1) {
        throw new TypeError("the number of array os over the max value restrocted!");
    }

    for(let i = 0; i < argCount; i++) {
        E[len + i] = items[i];
        len++;
    }
    O.length =  len;
    return len;
}

测试用例后面补上, 先手动测吧😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant