【发布时间】:2016-09-22 16:33:19
【问题描述】:
谁能告诉我为什么下面的开关不起作用?
var String=new String('String #1');
document.write(String);
document.write(' Check ');
switch(String)
{
case 'String #1' :
document.write('String Number 1');
break;
default: document.write('wrong string');
}
输出为:String #1 检查错误的字符串
【问题讨论】:
-
为什么将字符串命名为内置函数名?
-
^ 那,你应该得到
String is not a constructor错误 -
a) 不要创建
String实例 b) 不要覆盖Stringc) 不要在String对象上覆盖switch -
当我命名不同时,例如MyString,同样的事情仍然发生。
-
new String('String #1') !== 'String #1'。第一个是对象,第二个是原始值。是的,switch 使用严格的比较。
标签: javascript switch-statement case