在另一个函数中使用一个函数的变量——想想我遗漏了什么
Using a variable from one function in an other - think im missing something
这可能很简单,但我有点困难。是否可以执行以下操作,因为它在第二个函数中没有发出警报。
function func1() {
var test = 5;
var testAgain = 8;
}
function func3() {
func1();
alert(test);
}
编辑:
谢谢大家的评论,但我似乎无法让它发挥作用。
下面是我试图让这个在中工作的代码
它似乎不想传递setImageReelWidth中的vars,只是在它到达rotate=function()或$(".paging a").click(function()时提醒0。所以我点击它,它会提醒4(这是正确的),然后当它再次运行两次时,我只得到0
var divWidth = 0;
var slideTotal = 0;
var divReelWidth = 0;
function setImageReelWidth() {
var divWidth = $(".window").width();
var slideTotal = $(".slide").size();
var divReelWidth = divWidth * slideTotal;
alert(slideTotal);
//Adjust the image reel to its new size
$(".image_reel").css({ 'width': divReelWidth });
}
$(document).ready(function () {
//////////////////////////// INITAL SET UP /////////////////////////////////////////////
//Get size of images, how many there are, then determin the size of the image reel.
//var divWidth = $(".window").width();
//var slideTotal = $(".slide").size();
//var divReelWidth = divWidth * slideTotal;
//Adjust the image reel to its new size
//$(".image_reel").css({ 'width': divReelWidth });
//set the initial not active state
$('#prev').attr("class", "not_active");
//////////////////////////// SLIDER /////////////////////////////////////////////
//Paging + Slider Function
rotate = function () {
setImageReelWidth();
alert(slideTotal);
var triggerID = $slideNumber - 1; //Get number of times to slide
var image_reelPosition = triggerID * divWidth; //Determines the distance the image reel needs to slide
//sets the active on the next and prev
if ($slideNumber == 1) {
$('#prev').attr("class", "not_active")
$('#next').attr("class", "active")
}
else if ($slideNumber == slideTotal) {
$('#next').attr("class", "not_active");
$('#prev').attr("class", "active");
}
else {
$('#prev').attr("class", "active")
$('#next').attr("class", "active")
};
//Slider Animation
$(".image_reel").animate({
left: -image_reelPosition
}, 500);
};
//////////////////////////// SLIDER CALLS /////////////////////////////////////////////
//click on numbers
$(".paging a").click(function () {
setImageReelWidth();
alert(slideTotal);
setImageReelWidth();
$active = $(this); //Activate the clicked paging
$slideNumber = $active.attr("rel");
rotate(); //Trigger rotation immediately
return false; //Prevent browser jump to link anchor
});
像Rory在回答中建议的那样,通过全局定义变量,使变量对两种方法都可用,这很好。
另一个解决方案是从第一个函数返回它,并在第二个函数中使用它,如下所示:
function func1() {
var test = 5;
return test;
}
function func3() {
var x = func1();
alert(x);
}
编辑另一种方法是编写一个工厂函数来创建函数3:
var func3 = (function(){
var test;
function func1() {
test = 5;
}
return function() {
func1();
alert(test);
}
})();
这里发生的情况如下:我创建了一个立即执行的函数:
(function(){
// ...
})();
在那里,我有变量test
和另一个名为func1
的函数,并且I返回一个绑定到变量func3
的函数。func3
现在可以访问var test
和func1
,但我编写的工厂函数之外的任何东西都不能访问它们。
查看我创建的小提琴:在此处输入链接描述。
像这样的东西一开始有点难以理解,但它确实是JavaScript(IMHO)的强大之处。
您需要声明具有全局作用域的变量。
var test = 0;
function func1() {
test = 5;
}
function func3() {
func1();
alert(test);
}
进一步阅读javascript 中的变量作用域
相关文章:
- 访问嵌套函数结构中的JavaScript父函数变量
- Javascript函数变量传递语法问题
- JavaScript 函数变量范围问题
- 在 promise 中获取构造函数变量
- 将函数结果传递给Javascript中的另一个函数变量
- 用对象设置函数变量's值
- Javascript函数变量应生成错误,而不是未定义的错误
- 我可以访问用户脚本中的匿名函数变量吗?
- 在实例化期间使用构造函数变量
- 为什么在 JavaScript 中不能用函数变量覆盖字符串变量
- JavaScript 允许调用函数访问调用函数变量
- 如何在嵌套原型中访问 JavaScript 构造函数变量
- 对函数变量的 JavaScript 控制台日志输出感到困惑
- 如何将函数变量传递给主干模型上的 jQuery 事件
- 如何访问构造函数变量
- 实例函数变量不会更改
- 为什么函数变量在变量之后是未定义的
- 用内部函数变量更改外部函数变量?Javascript
- Javascript - 使用闭包递增静态函数变量模拟
- 异步 JavaScript 静态函数变量