Questions about the wrapper method of es6 class class

encapsulate a method with class:

const test=calss{
constructor(){
..
this.init()
}
..
init(){
  
 //return new Promise( function (resolve, reject) {})

}
}

init () is put into constructor, but when you go to execute new test (, is not a function, will prompt. Then is not a function, must remove this.init (), from constructor and then execute new test (and then execute new test (, and then how can it be executed properly? how can it be executed like this?

Jun.15,2021

return this.init ()

because you really don't have a then method defined, and you don't know why.



class Test {
  constructor() {
    return this.init()
  }

  init() {
    return new Promise((resolve, reject) => {
      console.log('I am a text class.')
      resolve();
    })
  }
}

new Test().then(a => console.log('hello world'))

return this.init ()

Menu