zoukankan      html  css  js  c++  java
  • 闭包案例 计算打车价格

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>
        <script>
            // 闭包应用 计算打车价格
            // function fn(){

            // }

            var car = (function() {
                var start = 13; // 起步价
                var total = 0; // 总价

                return {
                    // 正常的价格
                    price: function(n) {
                        if (n <= 3) {
                            total = start;
                        } else {
                            total = start + (n - 3) * 5;
                        }
                        return total;
                    },
                    // 拥堵之后的价格
                    yd: function(flag) {
                        return flag ? total + 10 : total;
                    }
                }
            })();
            console.log(car.price(5)); // 23
            console.log(car.yd(true)); // 33
            console.log(car.price(1)); // 13
            console.log(car.yd(false)); // 13
        </script>
    </body>

    </html>
  • 相关阅读:
    JS中关于clientWidth offsetWidth scrollWidth 等的含义
    javascript中数组concat()join()split()
    我的大数据学习路线(持续更新)
    java多线程-学习笔记
    java多线程-线程交互&互斥&同步
    java多线程-关键人物程咬金
    java多线程-军队战争
    java多线程-两个演员线程
    pytorch-Flatten操作
    龙良曲pytorch学习笔记_迁移学习
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13068491.html
Copyright © 2011-2022 走看看