在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ es6 class類封裝方法疑問

es6 class類封裝方法疑問

用class封裝一個方法:

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

}
}

init()放入了constructor,但是當去執(zhí)行new test(a,b,c).then()會提示.then is not a function,必須constructor里移除this.init(),然后執(zhí)行new test(a,b,c).init().then()才能正常執(zhí)行,用什么方法怎樣能讓它這樣new test(a,b,c).then()執(zhí)行?

回答
編輯回答
別逞強

return this.init()

因為你確實沒有定義then方法,而且不知道你為什么要這樣。。

2017年1月14日 22:40
編輯回答
念舊
class Test {
  constructor() {
    this.init()
  }

  init() {
    this._promise = new Promise(function(resolve, reject) {
      resolve();
    })
  }

  get then() {
    return this._promise.then.bind(this._promise);
  }
}

new Test().then(a => console.log('a', a))
2017年2月26日 14:01
編輯回答
安若晴
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() 即可

2017年12月26日 19:40