在jQuery中的多个函数之间传递变量?

时间:2014-08-08 12:29:48

标签: javascript jquery pass-by-reference

所以我有一些看起来像这样的代码:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

function checkmyresolutin() {
   // this tests the broswer/window resolution and print to the body [for css purposes]
  var resolution_is = '768px';
  my_important_var_x = resolution_is; // now the var contains the resolution

  return my_important_Var_X;
}

checkmyresolutin(); // for getting the resolutoin on document ready.

$(window).resize(function(){
   checkmyresolutin();  // this is the important part,every time the browser is resized i need to know ,certain function load/unload on certain resolutions.
});

function_abc(); // this is my problem,ill explain bellow,to keep this code block clean.

所以在浏览器调整大小后调用checkmyresolutin()并返回分辨率/ sceensize并存储在my_important_var_x变量中之后,我需要将该变量中的最新信息传递给{ {1}}功能......有人吗?

编辑:

伙计们,有拼写错误而忽略了它们,我没有复制粘贴我的.js,因为它需要多达400行,这只是一个 示例 , “逻辑”在这里很重要。

在jQuery function_abc();

运行enter code here之后,我只需要以某种方式将function_abc()的内容传递给checkmyresolutin()

1 个答案:

答案 0 :(得分:0)

你有一个错字:

var my_important_var_x = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

应该是

var my_important_Var_X = 'init'; // my global variable for storing one very important piece of info. [screen size/resolution]

看到这个JSFiddle,带有恼人的警报,以显示它正常工作。

我还使a different version在调整大小时自动调用function_abc(),以显示其工作原理。

在Javascript中查看变量范围的this discussion