gpt4 book ai didi

javascript - 在另一个函数中使用一个函数的变量——我想我遗漏了什么

转载 作者:行者123 更新时间:2023-11-30 07:10:33 25 4
gpt4 key购买 nike

这很可能很简单,但我有点挣扎是否可以做下面这样的事情,因为它在第二个函数中没有提醒。

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建议 his answer ,很好。

另一种解决方案是从第一个函数返回它并在第二个函数中使用它,如下所示:

function func1() {
var test = 5;
return test;
}


function func3() {
var x = func1();
alert(x);
}

编辑另一种方法是编写一个工厂函数来创建 func3:

var func3 = (function(){

var test;

function func1() {
test = 5;
}

return function() {
func1();
alert(test);
}


})();

这里发生的事情如下:我创建了一个立即执行的函数:

(function(){      
// ...
})();

在那里,我有变量 test 和另一个名为 func1 的函数,我返回一个绑定(bind)到变量 的函数>函数3func3 现在可以访问 var test 以及 func1 但在我编写的那个工厂函数之外什么都没有 可以访问它们

查看我创建的 fiddle :enter link description here .

像这样的东西一开始有点难以理解,但这确实是 JavaScript 的强大之处(恕我直言)。

关于javascript - 在另一个函数中使用一个函数的变量——我想我遗漏了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9174381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com