<bdo id='5iMib'></bdo><ul id='5iMib'></ul>

<small id='5iMib'></small><noframes id='5iMib'>

      <tfoot id='5iMib'></tfoot>
      <legend id='5iMib'><style id='5iMib'><dir id='5iMib'><q id='5iMib'></q></dir></style></legend>

    1. <i id='5iMib'><tr id='5iMib'><dt id='5iMib'><q id='5iMib'><span id='5iMib'><b id='5iMib'><form id='5iMib'><ins id='5iMib'></ins><ul id='5iMib'></ul><sub id='5iMib'></sub></form><legend id='5iMib'></legend><bdo id='5iMib'><pre id='5iMib'><center id='5iMib'></center></pre></bdo></b><th id='5iMib'></th></span></q></dt></tr></i><div id='5iMib'><tfoot id='5iMib'></tfoot><dl id='5iMib'><fieldset id='5iMib'></fieldset></dl></div>

      1. 当返回值在新行时,为什么 Javascript 返回语句不起作用?

        Why doesn#39;t a Javascript return statement work when the return value is on a new line?(当返回值在新行时,为什么 Javascript 返回语句不起作用?)

            <tbody id='FhQKn'></tbody>
            • <bdo id='FhQKn'></bdo><ul id='FhQKn'></ul>
            • <small id='FhQKn'></small><noframes id='FhQKn'>

            • <legend id='FhQKn'><style id='FhQKn'><dir id='FhQKn'><q id='FhQKn'></q></dir></style></legend>

                <tfoot id='FhQKn'></tfoot>

                  <i id='FhQKn'><tr id='FhQKn'><dt id='FhQKn'><q id='FhQKn'><span id='FhQKn'><b id='FhQKn'><form id='FhQKn'><ins id='FhQKn'></ins><ul id='FhQKn'></ul><sub id='FhQKn'></sub></form><legend id='FhQKn'></legend><bdo id='FhQKn'><pre id='FhQKn'><center id='FhQKn'></center></pre></bdo></b><th id='FhQKn'></th></span></q></dt></tr></i><div id='FhQKn'><tfoot id='FhQKn'></tfoot><dl id='FhQKn'><fieldset id='FhQKn'></fieldset></dl></div>
                • 本文介绍了当返回值在新行时,为什么 Javascript 返回语句不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下 JavaScript:

                  Consider the following JavaScript:

                  function correct()
                  {
                      return 15;
                  }
                  
                  function wrong()
                  {
                      return
                            15;
                  }
                  
                  console.log("correct() called : "+correct());
                  console.log("wrong() called : "+wrong());

                  上述代码片段中的 correct() 方法返回正确的值,在这种情况下为 15.wrong() 方法却返回 undefined.大多数其他语言并非如此.

                  The correct() method in the above code snippet returns the correct value which is 15 in this case. The wrong() method, however returns undefined. Such is not the case with the most other languages.

                  以下函数是正确的,并返回正确的值.

                  The following function is however correct and returns the correct value.

                  function wrong()
                  {
                      return(
                            15);
                  }
                  

                  如果语法错误,它应该会发出一些编译器错误,但它不会.为什么会这样?

                  If the syntax is wrong, it should issue some compiler error but it doesn't. Why does this happen?

                  推荐答案

                  从技术上讲,javascript 中的分号是可选的.但实际上它只是在某些换行符处为您插入它们,如果它认为它们丢失了.但它为您做出的决定并不总是您真正想要的.

                  Technically, semi colons in javascript are optional. But in reality it just inserts them for you at certain newline characters if it thinks they are missing. But the descisions it makes for you are not always what you actually want.

                  return 语句后跟一个新行告诉 JS 解释器应该在 return 之后插入一个分号.因此,您的实际代码是这样的:

                  And a return statement followed by a new line tells the JS intepreter that a semi colon should be inserted after that return. Therefore your actual code is this:

                  function wrong()
                  {
                      return;
                            15;
                  }
                  

                  这显然是错误的.那么为什么会这样呢?

                  Which is obviously wrong. So why does this work?

                  function wrong()
                  {
                       return(
                             15);
                  }
                  

                  好吧,这里我们用一个 open( 开始一个表达式.当 JS 找到新行时,它知道我们在一个表达式的中间,并且在这种情况下足够聪明,不会插入任何分号.

                  Well here we start an expression with an open(. JS knows we are in the middle of an expression when it finds the new line and is smart enough to not insert any semi colons in this case.

                  这篇关于当返回值在新行时,为什么 Javascript 返回语句不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                    <bdo id='E4PWB'></bdo><ul id='E4PWB'></ul>

                    <small id='E4PWB'></small><noframes id='E4PWB'>

                      1. <i id='E4PWB'><tr id='E4PWB'><dt id='E4PWB'><q id='E4PWB'><span id='E4PWB'><b id='E4PWB'><form id='E4PWB'><ins id='E4PWB'></ins><ul id='E4PWB'></ul><sub id='E4PWB'></sub></form><legend id='E4PWB'></legend><bdo id='E4PWB'><pre id='E4PWB'><center id='E4PWB'></center></pre></bdo></b><th id='E4PWB'></th></span></q></dt></tr></i><div id='E4PWB'><tfoot id='E4PWB'></tfoot><dl id='E4PWB'><fieldset id='E4PWB'></fieldset></dl></div>
                          <legend id='E4PWB'><style id='E4PWB'><dir id='E4PWB'><q id='E4PWB'></q></dir></style></legend>
                            <tbody id='E4PWB'></tbody>

                            <tfoot id='E4PWB'></tfoot>