es6 class静态成员

时间:2021-06-21 20:41:41   收藏:0   阅读:0

class静态成员

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>class静态成员</title>

</head>
<body>
<div id="ad">

</div>
<div></div>

<script>

    //ES5
    //手机类
    // function Phone(brand,price){
    //     this.brand = brand;
    //     this.price = price;
    // }
    // Phone.phoneName = ‘手机‘;
    // //添加方法
    // Phone.prototype.call = function (name) {
    //     return ‘打电话给‘ + name;
    // };
    //
    // let huaWei = new Phone(‘华为‘,5999);
    // console.log(huaWei);
    // console.log(huaWei.call(‘小明‘));
    // console.log(Phone.phoneName);

    //ES6
    class Phone{
        static phoneName = "手机";
        constructor(brand,price){
            this.brand = brand;
            this.price = price;
        }
        //方法
        static call(name){
            return ‘打电话给‘ + name;
        }
    }

    let apple = new Phone(‘苹果‘,8999);
    console.log(apple);
    console.log(Phone.call(‘小花‘));
    console.log(Phone.phoneName);

</script>
</body>
</html>
评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!