js 为Array实现一个Reader,通过接口getReader获取,Reader 有一个接口 read(n)()

为Array实现一个Reader,通过接口getReader获取,Reader 有一个接口 read(n)每次调用会按顺序读区数组的n(默认为1)个元素,调用不会改变数组本身的值,若数组已全部读取完则返回空数组,若传入的参数不为正整数则抛出异常例:

const arr = [1, 2, 3, 4, 5, 6]
const reader = arr.getReader()
console.log(reader.read(‘1’)) // Error
console.log(reader.read(-1)) // Error
console.log(reader.read(1.5)) // Error
console.log(reader.read()) // [1]
console.log(reader.read(1)) // [2]
console.log(reader.read(2)) // [3,4]
console.log(reader.read(3)) // [5,6]
console.log(reader.read()) // []

方案一,splice

Array.prototype.getReader = function () {
    //复制原数组
    let readArr = [...this];
    return {
        read(num) {
            //如果有参数,判断参数
            if (num) {
                try {
                    if (typeof num == "string") {
                        throw Error();
                    } else if (!/(^[1-9]\d*$)/.test(num)) {
                        //正则判断正整数 /^[1-9]\d*$/
                        throw Error();
                    } else {
                        return readArr.splice(0, num);
                    }
                } catch (e) {
                    return "Error";
                }
            } else {
                return readArr.splice(0, 1);
            }
        },
    };
};

方案二,slice

Array.prototype.getReader = function () {
    //拿到原数组
    let that = this;
    let count = 0;
    return {
        read(num) {
            //如果有参数,判断参数
            if (num) {
                try {
                    if (typeof num == "string") {
                        throw Error();
                    } else if (!/(^[1-9]\d*$)/.test(num)) {
                        //正则判断正整数 /^[1-9]\d*$/
                        throw Error();
                    } else {
                        let res = that.slice(count, count + num);
                        count = count + num;
                        return res;
                    }
                } catch (e) {
                    return "Error";
                }
            } else {
                //如果没有参数,默认为1
                if (that.length) {
                    let res = that.slice(count, count + 1);
                    count = count + 1;
                    return res;
                } else {
                    return [];
                }
            }
        },
    };
};
//实例
const arr = [1, 2, 3, 4, 5, 6];
const reader = arr.getReader();
console.log(reader.read("1")); // Error
console.log(reader.read(-1)); // Error
console.log(reader.read(1.5)); // Error
console.log(reader.read()); // [1]
console.log(reader.read(1)); // [2]
console.log(reader.read(2)); // [3,4]
console.log(reader.read(3)); // [5,6]
console.log(reader.read()); // []
————————

为Array实现一个Reader,通过接口getReader获取,Reader 有一个接口 read(n)每次调用会按顺序读区数组的n(默认为1)个元素,调用不会改变数组本身的值,若数组已全部读取完则返回空数组,若传入的参数不为正整数则抛出异常例:

const arr = [1, 2, 3, 4, 5, 6]
const reader = arr.getReader()
console.log(reader.read(‘1’)) // Error
console.log(reader.read(-1)) // Error
console.log(reader.read(1.5)) // Error
console.log(reader.read()) // [1]
console.log(reader.read(1)) // [2]
console.log(reader.read(2)) // [3,4]
console.log(reader.read(3)) // [5,6]
console.log(reader.read()) // []

方案一,splice

Array.prototype.getReader = function () {
    //复制原数组
    let readArr = [...this];
    return {
        read(num) {
            //如果有参数,判断参数
            if (num) {
                try {
                    if (typeof num == "string") {
                        throw Error();
                    } else if (!/(^[1-9]\d*$)/.test(num)) {
                        //正则判断正整数 /^[1-9]\d*$/
                        throw Error();
                    } else {
                        return readArr.splice(0, num);
                    }
                } catch (e) {
                    return "Error";
                }
            } else {
                return readArr.splice(0, 1);
            }
        },
    };
};

方案二,slice

Array.prototype.getReader = function () {
    //拿到原数组
    let that = this;
    let count = 0;
    return {
        read(num) {
            //如果有参数,判断参数
            if (num) {
                try {
                    if (typeof num == "string") {
                        throw Error();
                    } else if (!/(^[1-9]\d*$)/.test(num)) {
                        //正则判断正整数 /^[1-9]\d*$/
                        throw Error();
                    } else {
                        let res = that.slice(count, count + num);
                        count = count + num;
                        return res;
                    }
                } catch (e) {
                    return "Error";
                }
            } else {
                //如果没有参数,默认为1
                if (that.length) {
                    let res = that.slice(count, count + 1);
                    count = count + 1;
                    return res;
                } else {
                    return [];
                }
            }
        },
    };
};
//实例
const arr = [1, 2, 3, 4, 5, 6];
const reader = arr.getReader();
console.log(reader.read("1")); // Error
console.log(reader.read(-1)); // Error
console.log(reader.read(1.5)); // Error
console.log(reader.read()); // [1]
console.log(reader.read(1)); // [2]
console.log(reader.read(2)); // [3,4]
console.log(reader.read(3)); // [5,6]
console.log(reader.read()); // []