脚本标签之间的 JavaScript 函数作用域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10289593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:20:41  来源:igfitidea点击:

JavaScript function scope between script tags

javascripthtmljsp

提问by IAmYourFaja

I have two different JSPs that the Java backend concatenates together and sends back to the same rendered HTML page.

我有两个不同的 JSP,Java 后端将它们连接在一起并发送回相同的呈现的 HTML 页面。

Each JSP has its own <script>block and defines functions inside that block:

每个 JSP 都有自己的<script>块并在该块内定义函数:

JSP #1:

JSP#1:

<script type="text/javascript">
    function blah() { ... }
</script>

JSP #2

JSP#2

<script type="text/javascript">
    function foo()
    {
        blah();
    }
</script>

Like I said, the backend adds these to the HTTP response and sends them back to the browser during the same request.

就像我说的,后端将这些添加到 HTTP 响应中,并在同一请求期间将它们发送回浏览器。

When I run this page in my browser, I can tell right away that blah()is not executing when foo()is getting called. I see a console error in Firebug stating blah()is not defined. I'm wondering if blah()only has scope inside its own <script>tag, and likewise for foo(). Is that the case here, or is something else awry?

当我在浏览器中运行此页面时,我可以立即知道blah()foo()被调用时没有执行。我在 Firebug 中看到一个控制台错误,说明blah()未定义。我想知道是否blah()只有它自己的<script>标签内有范围,对于foo(). 这是这里的情况,还是其他问题?

When I go to view the page source I see both script blocks and both functions. This tells me everything is being generated/rendered correctly server-side, but perhaps my approach is inherently wrong (defining the functions inside different script tags). Thanks in advance.

当我去查看页面源代码时,我看到了两个脚本块和两个函数。这告诉我一切都在服务器端正确生成/呈现,但也许我的方法本质上是错误的(在不同的脚本标签中定义函数)。提前致谢。

采纳答案by Joseph

all of them are global. they can see each other. the problem is when they get defined and call each other.

所有这些都是全球性的。他们可以看到彼此。问题是当它们被定义并相互调用时。

you should define and call them in this order:

您应该按以下顺序定义和调用它们:

  1. bar
  2. foo
  3. call foo
    • foo executed and calls bar
    • bar is executed
  1. 酒吧
  2. 呼叫 foo
    • foo 执行并调用 bar
    • 酒吧被执行

回答by Amandeep Singh Bhamra

You can call function like this:

你可以这样调用函数:

  (function($) {

     var namespace;
         namespace = {
                     something : function() {
                                             alert('hello there!');
                                            },
                      bodyInfo : function() {
                                             alert($('body').attr('id'));
                                            }
                     };
         window.ns = namespace;
    })(this.jQuery);

   $(function() {
              ns.something();
              ns.bodyInfo();
   });

回答by Koen Peters

The only thing that defines scope in JavaScript is a function, so your problem is not a scoping issue. You most probably are not calling foo(), you call it before blah() is defined, or you have a syntax error somewhere. Maybe you can post your whole HTML page so we can see what's going on.

在 JavaScript 中唯一定义作用域的是函数,所以您的问题不是作用域问题。您很可能不是在调用 foo(),而是在定义 blah() 之前调用它,或者在某处出现语法错误。也许您可以发布整个 HTML 页面,以便我们了解发生了什么。