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

标签 javascript jquery

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

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/

相关文章:

javascript - Canvas 正在绘制旋转图像

Javascript识别卡号

javascript - 通过 NodeJS 将消息发布到 Firebase Cloud Messaging

javascript - 将鼠标悬停在一列中的图像上以使文本显示在不同的列中

javascript - 如何使用图像创建大复选框?

javascript - 如何在 Jquery Selector 中使用 c# ViewBag 变量?

javascript - 如何在悬停在菜单上时替换图像

当div扩展页面高度时javascript覆盖不覆盖整个页面

javascript - 使用 jQuery 将大列表拆分为三个等高列表

javascript - 如何在循环中为 jQuery 函数的变量设置动画?