如何在 Javascript 中使用 typeof 和 switch case
我无法找出下面的代码有什么问题。我咨询过如何使用 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";
}
}
-------------- 更新 -----------------------------------
转问题出在我没有正确遵循说明的错误(或者更确切地说是疏忽)。再次感谢您的帮助和评论!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
typeof
返回一个字符串,所以它应该是typeof
returns a string, so it should be