What's wrong with a JavaScript class whose constructor returns a function or an object

前端 未结 4 1737
忘掉有多难
忘掉有多难 2021-01-23 23:42

When I use new to instainciate an instance of a certain class, I got the actual instance. When the constructor function has a return value, the new sen

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 00:37

    The Javascript constructor does not need a return. This will work:-

    function foo() {
        this.x = 1;
    }
    
    var myfoo = new foo();
    console.log(myfoo.x);
    

提交回复
热议问题