Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问另一个函数中的变量_Javascript_Function_Variables - Fatal编程技术网

Javascript 如何访问另一个函数中的变量

Javascript 如何访问另一个函数中的变量,javascript,function,variables,Javascript,Function,Variables,我有一个计算屏幕宽度的函数 var screenWidth = jQuery(window).width(); function checkScreenSize() { screenWidth = jQuery(window).width(); } jQuery(window).on("resize", checkScreenSize).resize(); 如何在其他函数中使用此屏幕宽度 编辑: 我想在这里访问此变量,例如: function showContent()

我有一个计算屏幕宽度的函数

var screenWidth = jQuery(window).width();

function checkScreenSize()
{   
    screenWidth = jQuery(window).width();
}

jQuery(window).on("resize", checkScreenSize).resize();  
如何在其他函数中使用此屏幕宽度

编辑:

我想在这里访问此变量,例如:

function showContent() {
    if(screenWidth <= 1000)
    {
         console.log(screenWidth);
    }
}
函数showContent(){

如果(screenWidth因为
screenWidth
是在您提到的
函数
之外声明的全局变量,您可以按原样访问它:

function foo() {
    console.log(screenWidth);
}

您的全局
屏幕宽度
非常无用

如果需要知道调整大小处理程序回调中的宽度,则需要调用
jQuery(window).width()

函数checkScreenSize(){
var w=$(窗口).width()
控制台日志(w)
}
jQuery(窗口)。打开(“调整大小”,选中屏幕大小)
//.resize()是.on('resize'方法

当您在函数外定义变量时,它将是全局变量,您可以在其他函数中使用它!!
你对此有问题吗?

您还可以访问:

您可以通过在
global
范围中定义所述变量来实现这一点;如果
var screenWidth=…
是在其他函数上下文中定义的,您将无法从其他函数访问它

使用globals是个坏主意;但是,在许多情况下,您需要从其他地方访问一些变量值。在这种情况下,您可以构造一个定义良好的“globals”容器,例如:

'use strict';        // strict mode    :: prevents nasty bugs
this.Main = this;    // super-global   :: refers `window` -or- `global`

Main.Globals = {screenWidth:null};

// your dynamic screen resizing code here sets the `Globals.screenWidth`
// now you have access to this anywhere

您还可以编写一些代码,使此
Globals
对象仅可由某些函数写入,但此技术可能不在本问题的范围之内;不过,考虑到此代码是团队中有许多合作者的大型项目的一部分,这可能是一个好主意。

您在哪里尝试访问它,而不是一个问题Available?我想@Deep想知道的是,你在哪里定义这个函数,在哪里调用它。如果在checkScreenSize方法完成之前调用它,那么变量没有被设置为
width
值。你是否每次发生调整大小事件时都调用它?你如何同步这两个方法?如果
schowContent
的定义不在
var screenWith
变量的范围内(该变量不一定是“全局”变量;您可以在另一个函数体或requirejs模块中定义它)如果定义了,则
screenWith
将不为
showContent
方法所知。我应该说,当窗口变大时,我需要更新屏幕宽度also@KeithDonegan您的全局变量可以从任何地方访问。如果您需要在不同的操作中更新它,则需要为此添加处理程序。您可以已经有一个调整大小的处理程序。这怎么不起作用?你想实现什么?如果你需要一个更详细的答案,其中包括在窗口调整大小事件时动态设置值;或者即使我应该添加写入限制代码-只需在此处弹出一条注释,我会添加它;)