sta*_*tor 11 javascript typeof switch-statement
我在查找下面的代码有什么问题时遇到了问题.我已经咨询过如何使用typeof和switch case,但此时我已经迷路了.提前感谢您的建议.
// Write a function that uses switch statements on the
// type of value. If it is a string, return 'str'. If it
// is a number, return 'num'. If it is an object, return
// 'obj'. If it is anything else, return 'other'.
function detectType(value) {
switch (typeof value) {
case string:
return "str";
case number:
return "num";
default:
return "other";
}
}
Run Code Online (Sandbox Code Playgroud)
-------------更新-----------------------------------
事实证明,问题来自于我没有正确遵循指示的错误(或者说是疏忽).再次感谢您的帮助和评论!
qia*_*iao 24
typeof 返回一个字符串,所以它应该是
function detectType(value) {
switch (typeof value) {
case 'string':
return "str";
case 'number':
return "num";
default:
return "other";
}
}
Run Code Online (Sandbox Code Playgroud)