js中中括号代表什么意思??

新手上路,请多包涵
this[this.type === 'instance' ? 'updateInstance' : 'updateDefinition'](this.urlParam.id).then(res => {
                  // this.$message.success(res.msg)
                  this.$message({
                    message: res.msg,
                    type: 'success',
                    offset: 80
                  })
}

中括号又来圆括号后面打点then。。。什么意思,大佬们???

阅读 2k
2 个回答

这里三元表达式的结果是一个method方法名
this['method'] 即 this.method

所以
this[this.type === 'instance' ? 'updateInstance' : 'updateDefinition'](this.urlParam.id).then(...)

相当于

if (this.type === 'instance') {
    this.updateInstance(this.urlParam.id).then(...)
} else {
    this.updateDefinition(this.urlParam.id).then(...)
}

这个地方的括号是函数调用
调用this对象下面的updateInstance 或者 updateDefinition 方法,具体是哪个需要看 this对象的type值是否等于 instance

这两个方法返回的值是个promise对象,所以可能调用then方法;
当然不排除这两个方法返回的是其他带有then方法的对象

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