如何将来自不同函数的两个变量放入同一个流中

时间:2014-07-26 01:39:59

标签: javascript jquery jquery-ui math

我希望beginAge和rrspStart变量在同一个函数中输出而不是单独的函数,以便我可以在它们之间执行计算。到目前为止,我不知道如何做到这些我刚刚学会了如何在滑块幻灯片之外获取变量:函数本身。它们位于ageLog函数和startLog函数的底部。

$( document ).ready(function() {

// Individual Variables

var age;

$("#ageSlide").slider({
    value: 72,
    min: 35,
    max: 85,
    slide: function (event, ui) {
      age = ui.value;
      ageLog();
    }
});

// Individual Constants

var lifeLength = 100; // Life Expectancy

// Tax Constants

var mTr = 0.3; // Marginal Tax Rate
var dTr = 0.15; // Divided Tax Rate
var cGi = 0.5; // Capital Gains Inclusion for Tax

// RRSP Investment Variables

var rrspStart;

$( "#rrspStart" ).slider({
      value: 50000,
      min: 25000,
      max: 250000,
      slide: function( event, ui ) {
        rrspStart = ui.value;
        startLog();
      }
});

//RRSP Investment Constants

var rrspRoR = 0.6; // RRSP Rate of Return

//RSIP Investment Info

var rsipDiR = 0.4; // RSIP Interest Rate
var rsipRor = 0.6; // RSIP Rate of Return
var pRsipDcG = 0.7; // Percent RSIP Deferred Capital Gains 70%
var pRsipI = 0.1; // Percent RSIP Interest
var pRsipD = 0.1; // Percent RSIP Dividend
var pRsipCg = 0.1; // Percent RSIP Capital Gains

//RSIP Calculation


  function ageLog(){
    var period = 1;
    var beginAge = age;
    console.log(beginAge);
  }

  function startLog(){
    console.log(rrspStart);
  }

});

2 个答案:

答案 0 :(得分:2)

根据发布的代码。

函数beginAge中的

ageLog只是age

var beginAge = age;

age就像global variable rrspStart一样,也可以从startLog函数访问。

但它的(age)值会被以下代码更新

$("#ageSlide").slider({
    value: 72,
    min: 35,
    max: 85,
    slide: function (event, ui) {
      age = ui.value;
      ageLog();
    }
});

rrspStart的值由以下代码更新。

$( "#rrspStart" ).slider({
      value: 50000,
      min: 25000,
      max: 250000,
      slide: function( event, ui ) {
        rrspStart = ui.value;
        startLog();
      }
});

startLogageLog这两项功能分别由不同的elements调用。

而不是让两个functions创建单function

function ageAndStartLog(){
    console.log(rrspStart);
    console.log(beginAge);
}

并对用户交互使用相同的内容。

答案 1 :(得分:1)

我认为你可以像这样做

  function Log()
  {
    var period = 1;
    var beginAge = age;
    console.log("Begin age : " + beginAge);
    console.log(" rrspStart : " + rrspStart);
  }