2019-06-19 函数接收参数处理undefined的方式

不好的示范

spxq (businessKey, originatorId, relationTaskId, processInstanceId) {

      if (typeof businessKey === 'undefined') {

        businessKey = ''

      }

      if (typeof originatorId === 'undefined') {

        originatorId = ''

      }

      if (typeof relationTaskId === 'undefined') {

        relationTaskId = ''

      }

      if (typeof processInstanceId === 'undefined') {

        relationTaskId = ''

      }

     let openUrl = 'http://' + window.location.hostname + ':' + window.location.port + '/spxq.html?' +'businessKey=' +         businessKey + '&originatorId=' + originatorId + '&relationTaskId=' +relationTaskId + '&processInstanceId=' + processInstanceId+

'&tag=' + this.tag + '&path=wysp'

          window.open(openUrl, '_blank'

},

好的示范

不支持es6

    spxq (businessKey, originatorId, relationTaskId, processInstanceId) {

      let openUrl = 'http://' + window.location.hostname + ':' + window.location.port + '/spxq.html?'+

        'businessKey=' + ((typeof businessKey) === 'undefined' ? '' : businessKey) + '&originatorId=' + ((typeof originatorId) === 'undefined' ? '' : originatorId) + '&relationTaskId='+

        ((typeof relationTaskId) === 'undefined' ? '' : relationTaskId) + '&processInstanceId=' + ((typeof processInstanceId) === 'undefined' ? '' : processInstanceId) + processInstanceId+

        '&tag=' + ((typeof this.tag) === 'undefined' ? '' : this.tag) + '&path=wysp'

      window.open(openUrl, '_blank')

    },

支持es6

    spxq (businessKey = '', originatorId = '', relationTaskId = '', processInstanceId = '') {

      let openUrl = 'http://' + window.location.hostname + ':' + window.location.port + '/spxq.html?'+

        'businessKey=' + businessKey + '&originatorId=' + originatorId + '&relationTaskId=' + relationTaskId + '&processInstanceId=' + processInstanceId + '&tag=' + ((typeof this.tag) === 'undefined' ? '' : this.tag) + '&path=wysp'

      window.open(openUrl, '_blank')

    },

PS 一开始的时候想的是用三目运算符 在html中使用的结果报错了?是不是例如.toString等这些方法在html中都不能用?一般这种情况不应该处理传给函数的参数,而是应该在函数体中处理

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。