获取a中的另一个函数的变量

时间:2016-01-13 14:57:36

标签: javascript function variables

我有以下功能:

function charts(a, b, c){

//get the variables of function values and do some stuff with it

}

我通过a,b,c从另一个函数start得到变量:

function start(){
    var a = 1;
    var b = 2;
    var c = 3;
 charts(a, b, c);
 }

如何在功能图表中使用此功能的变量:

function values() {

    $.ajax({
        url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
        type: "GET",
        dataType: 'json',
        success: function (data) {
            value1 = data.value1;
            value2 = data.value2;
            value3 = data.value3;
        }
    });
}

1 个答案:

答案 0 :(得分:2)

function charts(a, b, c){
  //get the variables of function values and do some stuff with it
}

function values(){

  $.ajax({
    url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
    type: "GET",
    dataType: 'json',
    success: function(data){
      value1 = data.value1;
      value2 = data.value2;
      value3 = data.value3;
      charts(value1, value2, value3);
    }
  });
}

修改

如果您想将值传递给图表,然后调用ajax请求并在回调中使用abc,请尝试以下操作:

function charts(a, b, c) {
  return function () {
    $.ajax({
      url: site_url + "/?key=XXX&email=" + email + "&values=yes" + '&callback=?',
      type: "GET",
      dataType: 'json',
      success: function (data) {
        value1 = data.value1;
        value2 = data.value2;
        value3 = data.value3;
        console.log(a);
        console.log(b);
        console.log(c);
      }
    });
  }
}

用法:

var chartsFunction = charts(1,2,3);
chartsFunction();

输出:

1
2
3