Home > Blockchain >  how to access variable outside of my function?
how to access variable outside of my function?

Time:01-27

Im having trouble accessing my variable outside of my function. I don't have 'var' so I dont know how to access it in another function because the variables "rev_value" and "currency" come back undefined

//this function is being invoked first
FundraiseUp.on('donationComplete', function(details) {
  rev_value = details.donation.amount; //these are the variables i need to use outside of the function 
  currency = details.donation.currency; //and this one
});

function uet_report_conversion() {
  window.uetq = window.uetq || [];
  window.uetq.push('event', 'Donation', {
    "revenue_value": rev_value, //using them in this function
    "currency": currency //and here
  });
}

CodePudding user response:

The callback function provided to the FundraiseUp.on doesn't immediately gets executed. Therefore, the only way for the uet_report_conversion() function to access the data is to execute it after the donationComplete event triggers. The code would be something like this:

FundraiseUp.on('donationComplete', function(details) {  
        rev_value = details.donation.amount;
        currency = details.donation.currency;
        uet_report_conversion(rev_value, currency)
});
        
function uet_report_conversion(rev_value, currency) { 
window.uetq.....

CodePudding user response:

In JavaScript, variables declared within a function are only accessible within that function, and are not accessible outside of it. This is known as variable scoping. To access a variable outside of a function, you can do one of the following:

Declare the variable outside of the function: You can declare the variable rev_value and currency outside of the FundraiseUp.on function, and then assign them values inside the function. This way, the variables will be accessible outside of the function as well.

let rev_value;
let currency;
FundraiseUp.on('donationComplete', function(details) {  
    rev_value = details.donation.amount;
    currency = details.donation.currency;
});
function uet_report_conversion() { window.uetq = window.uetq || []; 
window.uetq.push('event', 'Donation', 
{"revenue_value":rev_value,"currency":currency}); } 

Pass the variables as arguments to the second function: You can pass the rev_value and currency variables as arguments to the uet_report_conversion function, and then access them inside the function.

    FundraiseUp.on('donationComplete', function(details) {  
    let rev_value = details.donation.amount;
    let currency = details.donation.currency;
    uet_report_conversion(rev_value, currency);
});
function uet_report_conversion(rev_value, currency) { window.uetq = 
window.uetq || []; window.uetq.push('event', 'Donation', 
{"revenue_value":rev_value,"currency":currency}); } 

You can use closure to make the variables accessible from inside the second function.

FundraiseUp.on('donationComplete', function(details) {  
    let rev_value = details.donation.amount;
    let currency = details.donation.currency;
    return function(){
        window.uetq = window.uetq || []; 
        window.uetq.push('event', 'Donation', 
{"revenue_value":rev_value,"currency":currency});
    }
});
let uet_report_conversion = FundraiseUp.on('donationComplete')();
  • Related