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

Using a variable from one function in an other - think im missing something

本文关键字:函数 变量 什么 另一个 一个      更新时间:2023-09-26

这可能很简单,但我有点困难。是否可以执行以下操作,因为它在第二个函数中没有发出警报。

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 testfunc1,但我编写的工厂函数之外的任何东西都不能访问它们

查看我创建的小提琴:在此处输入链接描述。

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

您需要声明具有全局作用域的变量。

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

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

进一步阅读javascript 中的变量作用域