如何从switch语句获取返回值?

编程入门 行业动态 更新时间:2024-10-25 05:28:50
本文介绍了如何从switch语句获取返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在chrome的控制台中,当我键入:

In chrome's console, when I type:

> switch(3){default:"OK"} "OK"

所以看起来switch语句具有返回值.但是当我这样做时:

So looks like the switch statement has a return value. But when I do:

> var a = switch(3){default:"OK"}

它引发语法错误意外的令牌切换"

It throws a syntax error "Unexpected Token switch"

是否可以捕获开关的return语句?

Is it possible to capture the return statement of the switch?

推荐答案

这是因为,当您将其放入Chrome控制台时,就会使其短路.它只是打印'OK',因为它达到了默认情况,而不是实际上返回任何内容.

That's because when you're putting that into the Chrome console, you're short-circuiting it. It's just printing 'OK' because it's reaching the default case, not actually returning something.

如果要返回某些内容,请将其粘贴到函数中,并在默认情况下返回确定".

If you want something returned, stick it in a function, and return the 'OK' from in the default case.

function switchResult(a){ switch(a){ default: return "OK"; } } var a = switchResult(3);

更多推荐

如何从switch语句获取返回值?

本文发布于:2023-10-11 22:29:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   返回值   switch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!