• 1js 高级


    <!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>
    </head>
    <body>
        <script>
            class Father {
                constructor(x,y) {
                    this.x = x;
                    this.y = y;
                }
                sum() {
                    console.log(this.x + this.y)
                }
            }
    
            class Son extends Father {
                constructor(x,y) {
                    super(x,y);
                    this.x = x;
                    this.y = y;
                }
                substract() {
                    console.log(this.x-this.y)
                }
            }
            var son1 = new Son(5,3)
            son1.substract()
            son1.sum()
        </script>
    </body>
    </html>

    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>
    </head>
    <body>
        <script>
            function Star(uname,age) {
                this.uname = uname;
                this.age = age;
                this.sing = function() {
                    console.log('我会唱歌')
                }
            }
            var ldh = new Star('刘德华',19)
            var zxy = new Star('张学友',23)
            console.log(ldh);
            ldh.sing();
            zxy.sing();
        </script>
    </body>
    </html>

    实例成员就是带this的成员,只能通过实例化对象访问

    静态成员只能通过构造函数访问

    但是构造函数存在浪费内存的问题,所以引入原型对象prototype的概念,实现方法共享

    <!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>
    </head>
    <body>
        <script>
            function Star(uname,age) {
                this.uname = uname;
                this.age = age;
               
                }
            }
            Star.prototype.sing = function() {
                consloe.log('我会唱歌')
            }
            var ldh = new Star('刘德华',19)
            var zxy = new Star('张学友',23)
            ldh.sing();
            zxy.sing();
        </script>
    </body>
    </html>

     

    原型链

  • 相关阅读:
    海量数据与布隆过滤
    Flink History Job
    golang and intellij
    maven中如何得到父工程的位置
    maven中进行go的编译
    hbase表的写入
    Storm之tickTuple
    storm-kafka版本不匹配的问题
    (17)zabbix自定义用户key与参数User parameters
    (16)zabbix history trends历史与趋势数据详解
  • 原文地址:https://www.cnblogs.com/gao-chao/p/13402249.html
Copyright © 2020-2023  润新知