期望一个条件表达式,而是看到一个赋值 [英] Expected a conditional expression and instead saw an assignment

查看:48
本文介绍了期望一个条件表达式,而是看到一个赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript代码具有以下功能:

I have the following function in my javascript code:

  addParam(url, param, value) {
    var a = document.createElement('a'), regex = /(?:\?|&|&)+([^=]+)(?:=([^&]*))*/g;
    var match, str = []; a.href = url; param = encodeURIComponent(param);

    while(match = regex.exec(a.search)) {
      if(param != match[1]) {
        str.push(match[1] + (match[2] ? '=' + match[2] : ''));
      }
    }

    str.push(param + (value ? '=' + encodeURIComponent(value) : ''));
    a.search = str.join('&');
    return a.href;
  }

eslint返回以下错误:

eslint returns me following error:

  306:15  error  Expected a conditional expression and instead saw an assignment  no-cond-assign

问题是while语句:

The problem is this while statement:

while(match = regex.exec(a.search)) {
  if(param != match[1]) {
    str.push(match[1] + (match[2] ? '=' + match[2] : ''));
  }
}

我该如何改写以解决此问题?

How can I rewrite this to fix that?

推荐答案

您要执行的操作:

while((match = regex.exec(a.search))) {
  if(param != match[1]) {
    str.push(match[1] + (match[2] ? '=' + match[2] : ''));
  }
}

您需要额外的括号

这篇关于期望一个条件表达式,而是看到一个赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆