js class 方法中修改this指向

const base = require("./base")

class doc extends base {
    constructor(connecturl) {
        super(connecturl)
        this.test = 'uuu'
        this.DataFromDb = null
        this.DataFromUser = null
        this.isExistsDocEnv = null
        this.isNotExistsDocEnv = null
    }

    async DocExistsByuuidkey(uuid, isNotExistsDocEnv, isExistsDocEnv) {
        if(typeof isExistsDocEnv  === 'function' )   this.isExistsDocEnv  = isExistsDocEnv
        if(typeof isNotExistsDocEnv  === 'function' )   this.isNotExistsDocEnv  = isNotExistsDocEnv

        this.db.query(
            'select * from doc.doc_attr where uuidkey = $1 limit 1', uuid
        ).then((data) =>{
            if(data == null) this.isNotExistsDocEnv.apply(this, data) 
            else this.isExistsDocEnv.apply(this, data)
        } ).catch((err) => {
            throw err
        })

    }
    
    async save() {

    }
     

}
module.exports = doc

让调用this.isNotExistsDocEnv.apply(this, data) 是
this指向对象的this

阅读 3.9k
1 个回答
async DocExistsByuuidkey(uuid, isNotExistsDocEnv, isExistsDocEnv) {
*       if(typeof isExistsDocEnv  === 'function' )   this.isExistsDocEnv  = isExistsDocEnv.bind(this)
*       if(typeof isNotExistsDocEnv  === 'function' )   this.isNotExistsDocEnv  = isNotExistsDocEnv.bind(this)

        this.db.query(
            'select * from doc.doc_attr where uuidkey = $1 limit 1', uuid
        ).then((data) =>{
*           if(data == null) this.isNotExistsDocEnv 
*           else this.isExistsDocEnv
        } ).catch((err) => {
            throw err
        })

    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题