jQuery 生成全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18995647/
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-08-26 22:56:00  来源:igfitidea点击:

jQuery make global variable

javascriptjquery

提问by olo

How to pass function a_href = $(this).attr('href');value to global a_href, make a_href="home"

如何将函数a_href = $(this).attr('href');值传递给 global a_href,使a_href="home"

var a_href; 

    $('sth a').on('click', function(e){
        a_href = $(this).attr('href');

          console.log(a_href);  
         //output is "home"

        e.preventDefault();
    }

console.log(a_href);  
//Output is undefined 

回答by Arun P Johny

Your code looks fine except the possibility that if the variable declaration is inside a dom read handler then it will not be a global variable... it will be a closure variable

您的代码看起来不错,但如果变量声明在 dom 读取处理程序内,则它不会是全局变量......它将是一个闭包变量

jQuery(function(){
    //here it is a closure variable
    var a_href;
    $('sth a').on('click', function(e){
        a_href = $(this).attr('href');

          console.log(a_href);  
         //output is "home"

        e.preventDefault();
    }
})

To make the variable global, one solution is to declare the variable in global scope

要使变量成为全局变量,一种解决方案是在全局范围内声明变量

var a_href;
jQuery(function(){
    $('sth a').on('click', function(e){
        a_href = $(this).attr('href');

          console.log(a_href);  
         //output is "home"

        e.preventDefault();
    }
})

another is to set the variable as a property of the window object

另一种是将变量设置为window对象的一个​​属性

window.a_href = $(this).attr('href')

Why console printing undefined

为什么控制台打印未定义

You are getting the output as undefinedbecause even though the variable is declared, you have not initialized it with a value, the value of the variable is set only after the aelement is clicked till that time the variable will have the value undefined. If you are not declaring the variable it will throw a ReferenceError

您正在获得输出,undefined因为即使声明了变量,您还没有使用值对其进行初始化,只有在a单击元素后才设置变量的值,直到该变量具有 value 为止undefined。如果你没有声明变量,它会抛出一个ReferenceError

回答by gp.

set the variable on window:

在窗口上设置变量:

window.a_href = a_href;

回答by RobG

You can avoid declaration of global variables by adding them directly to the global object:

您可以通过将全局变量直接添加到全局对象来避免声明全局变量:

(function(global) {

  ...

  global.varName = someValue;

  ...

}(this));

A disadvantage of this method is that global.varNamewon't exist until that specific line of code is executed, but that can be easily worked around.

这种方法的一个缺点是global.varName在执行该特定代码行之前不存在,但可以轻松解决。

You might also consider an application architecture where such globals are held in a closure common to all functions that need them, or as properties of a suitably accessible data storage object.

您还可以考虑这样一种应用程序架构,其中此类全局变量保存在所有需要它们的函数通用的闭包中,或者作为适当可访问的数据存储对象的属性。