• ES6学习笔记四(类和对象)


    {
        // 构造函数和实例
        class Parent{
            constructor(name='mukewan'){
                this.name=name;
            }
        }
        let v_parent=new Parent('v');
        console.log('构造函数和实例',v_parent);   //构造函数和实例 Parent {name: "v"}
    }
    
    {
        // 继承
        class Parent{
            constructor(name='mukewan'){
                this.name=name;
            }
        }
    
        class Child extends Parent{
    
        }
        console.log('继承',new Child());  //继承 Child {name: "mukewan"}
    }
    {
        // 继承传递参数
        class Parent{
            constructor(name='mukewan'){
                this.name=name;
            }
        }
    
        // 子类Child继承父类Parant
        class Child extends Parent{
            constructor(name='child'){
                super(name);  //修改父类属性,一定要放在构造函数的第一行
                this.type='child';
            }
        }
    
        console.log('继承传递参数',new Child('hello'));  //继承传递参数 _Child {name: "hello", type: "child"}
    }

    getter(读取)、setter(设置)属性

    {
        // getter,setter
        class Parent {
            constructor(name='mukewan'){
                this.name=name;
            }
    
            get longName(){  //读取
                return 'mk'+this.name;
            }
    
            set longName(value){  //设置
                this.name=value;
            }        
        }
    
        let v=new Parent();
        console.log('getter',v.longName);  // getter mkmukewan
        v.longName='hello';
        console.log('setter',v.longName);  //setter mkhello
    }

    static静态方法跟静态属性

    {
        // 静态方法
        class Parent {
            constructor(name='mukewan'){
                this.name=name;
            }
    
            static tell(){  //static+函数名,
                 console.log('tell');   
            }        
        }
    
        Parent.tell();  //tell  静态方法是通过类去调用,而不是实例去调用!
    
    }
    
    {
        // 静态属性
        class Parent {
            constructor(name='mukewan'){
                this.name=name;
            }
    
            static tell(){  //static+函数名,
                 console.log('tell');   
            }        
        }
    
        Parent.type='test'  //这边使用的都是类,而不是实例
    
        console.log('静态属性',Parent.type);  //静态属性 test
    
    }
  • 相关阅读:
    react父子组件之间传值
    MVC、MVP、MVVM模式的概念与区别
    exports、module.exports 和 export、export default
    进程与线程以及它们的区别
    axios详解
    箭头函数详解
    ES6扩展运算符...
    vue子组件数据跟着父组件改变
    JS实现千分位
    在.NET Core使用TimeZone将客户端时间转服务器本地时间但编译提示已过期
  • 原文地址:https://www.cnblogs.com/qdlhj/p/9930878.html
Copyright © 2020-2023  润新知