美文网首页
2019-06-19 函数接收参数处理undefined的方式

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

作者: qiaoguoxing | 来源:发表于2019-06-19 16:17 被阅读0次

    不好的示范

    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中都不能用?一般这种情况不应该处理传给函数的参数,而是应该在函数体中处理

    相关文章

      网友评论

          本文标题:2019-06-19 函数接收参数处理undefined的方式

          本文链接:https://www.haomeiwen.com/subject/hdvkqctx.html