Javascript var语句中可接受的字符

Javascript var语句中可接受的字符,javascript,Javascript,我编写了一段代码,当我使用特定标记时,它会显示一个单词: <script type='text/javascript'> var {type} = '{type}'; if ({type} == 'football'){ document.write('football'); } </script> 谢谢。您似乎在使用某种伪代码-{type}看起来像一种模板语言 在JavaScript中,这将起作用-请注意引号嵌套 var ty

我编写了一段代码,当我使用特定标记时,它会显示一个单词:

<script type='text/javascript'>
    var {type} = '{type}';

    if ({type} == 'football'){
        document.write('football');
    }
</script>

谢谢。

您似乎在使用某种伪代码-
{type}
看起来像一种模板语言

在JavaScript中,这将起作用-请注意引号嵌套

var type = "women's football"; 
if (type == 'football' || type == "women's football") {
  document.write('football');
}
这也会起作用

var types = {
  football : ["football","women's football"]
}

alert(types["football"][1])

这回答了你的问题吗?我觉得问题的标题和问题的内容非常不同,除非你的目标js引擎支持解构这个
var{type}='{type}'不是有效的javascript。即使是这样。然后您将“始终”得到未定义的
type
而不是
var{type}=“{type}”。。。不能在变量名中使用大括号。您可以在值中随意使用它们,但如前所述,使用除$、\或字母数字字符以外的任何字符声明var几乎是不允许的。我尝试过这样做,但只有在用花括号表示建议时才有效。我可以在站点外部工作,但不能在站点内部工作,因此必须将
type
还原为
{type}
。结果是现在有了一个输出,但它为每种类型的文章输出了“足球”,而不仅仅是“足球”和“女子足球”。正如我所说,该网站可能运行类似Smarty的模板。
var type = "women's football"; 
if (type == 'football' || type == "women's football") {
  document.write('football');
}
var types = {
  football : ["football","women's football"]
}

alert(types["football"][1])