构造函数调用的返回值问题

构造函数调用会创建一个新对象,一般不使用return,会默认返回当前对象
使用return会有2种情况
1.返回一个新的对象,则会变成该新对象
2.返回一个原始值,则还是返回当前对象,不会返回该原始值
const Obj1 = function(){
    this.name = "abc"
}
const Obj2 = function(){
    this.name = "abc"
    return {
        name: "bbb"
    }
}
const Obj3 = function(){
    this.name = "abc"
    return this.name
}

const obj1 = new Obj1()    // Obj1 { name: 'abc' }
const obj2 = new Obj2()    // { name: 'bbb' }
const obj3 = new Obj3()    // Obj3 { name: 'abc' }

 

posted @ 2021-12-13 21:01  邢韬  阅读(93)  评论(0编辑  收藏  举报