將數量限制為 MinMax 範圍

如果需要鉗位數字以使其保持在特定範圍邊界內

function clamp(min, max, val) {
    return Math.min(Math.max(min, +val), max);
}

console.log(clamp(-10, 10, "4.30")); // 4.3
console.log(clamp(-10, 10, -8));     // -8
console.log(clamp(-10, 10, 12));     // 10
console.log(clamp(-10, 10, -15));    // -10

用例示例(jsFiddle)