美文网首页
面向对象

面向对象

作者: asmuzi | 来源:发表于2018-12-21 14:10 被阅读0次
    es6面向对象
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            // function User(name, pass) {
            //     this.name = name;
            //     this.pass = pass;
            // }
    
            // User.prototype.login = function () {
            //     console.log('登录成功');
            // }
    
            class User {
                constructor(name, pass) {
                    this.name = name;
                    this.pass = pass;
                }
    
                login() {
                    console.log('登录成功');
                }
            }
    
            let p = new User('zs', 123);
            console.log(p.name);
            console.log(p.pass);
            p.login();
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    
    es6面向对象2
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            // function User(name, pass) {
            //     this.name = name;
            //     this.pass = pass;
            // }
    
            // User.prototype.login = function () {
            //     console.log('登录成功');
            // }
    
            class User {
                constructor(name, pass) {
                    this.name = name;
                    this.pass = pass;
                }
    
                login() {
                    console.log('登录成功');
                }
            }
    
            let p = new User('zs', 123);
            console.log(p.name);
            console.log(p.pass);
            p.login();
    
            class VIPUser extends User {
                constructor(name, pass, level) {
                    super(name, pass);
    
                    this.level = level;
                }
    
                download() {
                    console.log('下载');
                }
            }
    
    
            let v = new VIPUser('ls', '123456', 8);
    
            v.login();
            v.download();
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    
    es6面向对象3
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script>
            // function User(name, pass) {
            //     this.name = name;
            //     this.pass = pass;
            // }
    
            // User.prototype.login = function () {
            //     console.log('登录成功');
            // }
    
            class User {
                constructor(name, pass) {
                    this.name = name;
                    this.pass = pass;
                }
    
                login() {
                    console.log('登录成功');
                }
            }
    
            let p = new User('zs', 123);
            console.log(p.name);
            console.log(p.pass);
            p.login();
    
            class VIPUser extends User {
                constructor(name, pass, level) {
                    super(name, pass);
    
                    this.level = level;
                }
    
                download() {
                    console.log('下载');
                }
            }
    
    
            let v = new VIPUser('ls', '123456', 8);
    
            v.login();
            v.download();
    
            console.log(v instanceof User);
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:面向对象

          本文链接:https://www.haomeiwen.com/subject/mmvmkqtx.html