美文网首页
ES6 class类的函数实现return

ES6 class类的函数实现return

作者: 学无止境_cheer_up | 来源:发表于2022-04-22 20:51 被阅读0次
     // 创建类
            class Person {
                constructor(name,age,hobby){
                    this.name = name
                    this.age = age
                    this.hobby = hobby
                }
            }
            let LDH = new Person('刘德华',18,'唱歌')
            console.log(LDH) 
    
            //  继承类  
            class Man extends Person {
                constructor(name,age,hobby,say){
                    super(name, age, hobby)
                    this.say = say
                }
            }
            let XM = new Man('小明',20,'跳舞','说话')
            console.log(XM)
    
            function Person(name,age,hobby){
                // 判断this是否是Person的实例对象
                // 如果不是,说明是通过Person()来调用的
                // 此时,我们动手通过new来创建实例对象
                if(!(this instanceof Person)){
                    return new Person(name,age,hobby)
                }
                this.name = name
                this.age = age
                this.hobby = hobby
            }
            const P1 = new Person()
            const P2 = Person()
    

    相关文章

      网友评论

          本文标题:ES6 class类的函数实现return

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