es6 class

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.prototype.call = function (name) {
    //     return '打电话给' + name;
    // };
    //
    // let huaWei = new Phone('华为',5999);
    // console.log(huaWei);
    //console.log(huaWei.call('小明'));

    //ES6
    class Phone{
        constructor(brand,price){
            this.brand = brand;
            this.price = price;
        }
        //方法
        call(name){
            return '打电话给' + name;
        }
    }

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

</script>
</body>
</html>
posted @ 2021-06-21 12:27  胡勇健  阅读(32)  评论(0)    收藏  举报