简体   繁体   English

JavaScript中的全局变量无法按预期工作

[英]Global variable in javascript doesn't work as expected

here is the code, I have a menu global variable inside the function. 这是代码,我在函数内部有一个菜单全局变量。 I want to use it outside it, but then I get an "undefined reference error...· This is the only javascript code I have, so there's no interference with another variables or functions. 我想在它之外使用它,但是随后出现“未定义的引用错误...·”这是我仅有的javascript代码,因此不会干扰其他变量或函数。

Thanks in advance. 提前致谢。

$(function() {
  menu =  $('nav#menu').mmenu(
    {   
      navbars: [
        {
          position: "top",  
          height    : 1,
          content : [ 
            '<a href="#/" class="fa fa-phone"></a>',
            '<img src="imagenes/wheel32.png" /><p class="navbartxt">bicimap.uy</p>',                    
          ]
        },
        {
          position: "bottom",
          content: [
            '<a class="fa fa-envelope"></a>',
            '<a class="fa fa-twitter"></a>',
            '<a class="fa fa-facebook"></a>'
          ]
        }
      ],                    
      extensions: ["multiline"],    
      onClick: {
        close: false
      },
      navbar:{
        title: "Inicio"                     
      },
      offCanvas: {           
        zposition : "next"
      }
    });         
  });

I need to put this inside the function to get it working 我需要将其放在函数中以使其正常工作

var API = $("#menu").data( "mmenu" );

menu.on( 'click', 'a[class^="fa fa-twitter"]', function() {  
  $('#twitter').show();                 
  var API = $("#menu").data( "mmenu" );
  API.close();      

  return false;
});

I'm lacking some context here (I'm assuming there's more we don't see?) 我在这里缺少上下文(我假设还有更多我们看不到的东西?)

But you may be executing the latter snippet, menu.on( 'click'… before the code inside $(…) has run (on dom ready); so menu would be undefined. 但是您可能正在运行$(…)内部的代码(在dom就绪)之前执行后一个代码段menu.on( 'click'… );因此menu将是未定义的。

If you're just getting started, it's worth looking into your browser's developer tools and learning about break points and logging. 如果您只是入门,那么值得研究浏览器的开发人员工具并了解断点和日志记录。

You'll need to make sure that you use menu only after it's been set, likely by delaying all your invocation to be on ready. 您需要确保仅在设置menu后才使用menu ,这可能会延迟所有准备就绪的调用。


Execution Order 执行命令

  1. The $ is called and the function is provided as the first and only argument. $被调用,并且函数作为第一个也是唯一的参数提供。 The function itself is not called. 该函数本身不被调用。

  2. API is defined by the result of $("#menu" ) This may or may not be found yet depending upon where in the html file you've added this script. API$("#menu" )的结果定义。取决于是否在此文件的html文件中添加了该位置,可能无法找到。

  3. The data method is invoked on the result of that data方法是根据该方法的结果调用的

  4. You try to call on on menu , which is still undefined 您尝试调用onmenu ,这仍然是不确定的

  5. sometime later, the function you passed to $ is called, and menu ends up being defined, to late to be useful 稍后,您传递给$的函数被调用,菜单最终被定义,直到后来才有用

If your closure/anonymous function is happening asynchonously, its possible that the work has not been done by the time the rest of your code is computed. 如果您的闭包/匿名函数是异步发生的,则可能在计算其余代码时尚未完成工作。 I would need more context to determine that. 我需要更多的上下文来确定这一点。

This is the solution I found, maybe it's not the best because it could bring problems. 这是我找到的解决方案,可能不是最好的解决方案,因为它可能会带来问题。 For some reason the ready function doesn't work as is shown above, maybe some assets are being expected. 由于某些原因,就绪功能无法如上所示工作,可能是预期会有一些资产。

menu = null;
$(function() {

            menu =  $('nav#menu').mmenu(

            {   
                navbars     : [{
                    position: "top",    
                    height  : 1,
                    content : [ 

                        '<img src="imagenes/wheel32.png" /><p class="navbartxt">bicimap.uy</p>',                

                    ]
                },
                {
                    position: "bottom",
                    content: [
                        '<a class="fa fa-envelope"></a>',
                        '<a class="fa fa-twitter"></a>',
                        '<a class="fa fa-facebook"></a>'
                    ]
                }
                ],                  
                extensions: ["multiline"],  
                 onClick: {
                    close: false
                 },
                navbar:{
                    title: "Inicio"                     
                },
                offCanvas: {
                  zposition : "next"
                }

        });

    });


$( window ).load(function() {

  var API = $("#menu").data( "mmenu" );

        menu.on( 'click', 'a[class^="fa fa-twitter"]', function() {  
                        $('#twitter').show();

                          var API = $("#menu").data( "mmenu" );
                          API.close();      

                        return false;
                    }
         );

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM