📜  javascript代码示例中的范围

📅  最后修改于: 2022-03-11 15:02:45.693000             🧑  作者: Mango

代码示例6
// while ES6 does not have any similar methods built-in 
// the same basic functionality can be implemented by:
[...Array(5).keys()];
 => [0, 1, 2, 3, 4] 

String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0)));
 => "ABCD"

//               OR             //

function range(size, startAt = 0) {
    return [...Array(size).keys()].map(i => i + startAt);
}

function characterRange(startChar, endChar) {
    return String.fromCharCode(...range(endChar.charCodeAt(0) -
            startChar.charCodeAt(0), startChar.charCodeAt(0)))
}