美文网首页
ES6 7.类和对象

ES6 7.类和对象

作者: Coldhands | 来源:发表于2018-07-21 20:06 被阅读0次
{
    class Parent {
        constructor(name = 'binbin'){
            this.name = name;
        }
    }
    let parent = new Parent('v');
    console.log('构造函数和实例',parent);
}

输出结果:构造函数和实例 Parent {name: "v"}

{
    //继承
    class Parent{
        constructor(name='binbin'){
            this.name = name;
        }
    }
    class Child extends Parent{
        constructor(name = 'child'){
            super(name);
            this.type = 'child';
        }
    }
    console.log('继承传递参数',new Child('hello'));
}

输出结果:继承传递参数 Child {name: "hello", type: "child"}

{
    // getter,setter
    class Parent{
        constructor(name='binbin'){
            this.name = name;
        }
        get longName(){
            return 'wang' + this.name;
        }
        set longName(value){
            this.name = value;
        }
    }
    let parent = new Parent();
    console.log('getter',parent.longName);
    parent.longName = 'li';
    console.log('setter',parent.longName);
}

输出结果:getter wangbinbin
     setter wangli

{
    // 静态属性
    class Parent{
        constructor(name="binbin"){
            this.name = name;
        }
        static tell(){
            console.log('tell');
        }
    }
    Parent.tell(); //采用类调用,并非new Parent()实例调用
    Parent.type = 'vava';
    console.log(Parent.type);
}

输出结果:tell
     vava

相关文章

网友评论

      本文标题:ES6 7.类和对象

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