Js6利用class创建类

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6     </head>
 7     <body>
 8         <h1>结算程序</h1>
 9         <p>
10             <input type="text" placeholder="原价" id="money" />
11         </p>
12         <p>
13             <select id="level">
14                 <option value="0">普通顾客</option>
15                 <option value="1">VIP会员</option>
16                 <option value="2">金卡会员</option>
17                 <option value="3">砖石会员</option>
18             </select>
19         </p>
20         <p>
21             <input type="button"id="btn" value="结算" />
22         </p>
23         <p id="result"></p>
24         <script src="test_3es6.js" type="text/javascript" charset="utf-8"></script>
25     </body>
26 </html>
 1 /*
 2 //定义类
 3 class Plane{
 4     constructor(speed) {
 5         this.speed=speed;//变量放在构造器中
 6     }
 7     fly(){
 8         console.log("飞行速度"+this.speed);
 9     }
10 }
11 var plane=new Plane(800);
12 plane.fly();
13 //类继承
14 class FightPlane extends Plane{
15     fly(){
16         console.log("战斗机速度"+this.speed);
17     }
18 }
19 plane =new FightPlane(2000);//多态
20 plane.fly();
21 */
22 
23 /*
24  *正常情况下普通顾客不享受打折优惠,VIP会员享受9.6折优惠,金卡会员享受9.2折优惠,砖石会员享受8.5折优惠。
25  *     双十一当天,普通顾客享受满200返50代金券;
26  *     VIP会员享受9折优惠,购物满500,赠送100元餐券;
27  *     金卡会员享受8.5折优惠,购物满300元,赠送100元代金券;
28  *     砖石会员享受8折优惠,购物满200赠送100代金券。
29  */
30 class Calculator {
31     handle(money, level) { //处理
32         if(level == 0) {
33             return money;
34         } else if(level == 1) {
35             return money * 0.96;
36         } else if(level == 2) {
37             return money * 0.92;
38         } else if(level == 3) {
39             return money * 0.85;
40         } else {
41             throw "会员等级错误";
42         }
43     }
44 }
45 
46 class DoubleElevenCalculator extends Calculator {
47     handle(money, level) { //处理
48         if(level == 0) {
49             var num = Math.floor(money / 200);
50             return `实际需支付${money}元,赠送${num}张50元代金券`;
51         } else if(level == 1) {
52             var num = Math.floor(money / 500);
53             return `实际需支付${money*0.9}元,赠送${num}张100元餐券`
54         } else if(level == 2) {
55             var num = Math.floor(money / 300);
56             return `实际需支付${money*0.85}元,赠送${num}张100元代金券`;
57         } else if(level == 3) {
58             var num = Math.floor(money / 200);
59             return `实际需支付${money*0.8}元,赠送${num}张100元代金券`;
60         } else {
61             throw "会员等级错误";
62         }
63     }
64 
65 }
66 
67 var btn = document.querySelector("#btn");
68 btn.addEventListener("click", function() {
69     //var calculator = new Calculator();
70     var calculator = new DoubleElevenCalculator();
71     var money = document.querySelector("#money").value;
72     money = new Number(money);
73     var level = document.querySelector("#level").value;
74     level = new Number(level);
75     var temp = calculator.handle(money, level);
76     document.querySelector("#result").innerHTML = temp;
77 });
原文地址:https://www.cnblogs.com/liuxuanhang/p/7800981.html