01_09.函数的优化

2017-11-13  本文已影响0人  Robyn_Luo
 <script>
    // 获取一个任意区间的整数随机数,默认求0~100之间的随机数
    // 以前定义参数默认值都是通过||运算符实现的
    // function random(min, max) {
    //     min = min || 1;
    //     max = max || 100;
    //     return  Math.floor(Math.random() * (max - min) + min);
    // }
    // console.log(random(2, 10));

    // es6对方法的默认值提供了支持
    function random(min = 1, max = 100) {
        return  Math.floor(Math.random() * (max - min) + min);
    }
    console.log(random());
    </script>
上一篇 下一篇

猜你喜欢

热点阅读