Cadell Teng
Cadell Teng

Reputation: 244

How do I export a private variable to a public variable in javaScript (Node.Js)

For example, I have a calculation program like this:

function cal(salary, additionalSalary) {
  var salary;
  var additionalSalary;
  var totalPay = salary + additionalSalary;
}

function 401k() {
  var specialAccount = totalPay * 0.37;
  var normalAccount = totalPay - specialAccount;
}

My question is how do I have the totalPay variable be exported out from the function to make it a public variable to be used in 401k function. Thanks.

Upvotes: 3

Views: 724

Answers (3)

Mauro López
Mauro López

Reputation: 109

You have multiple options, so choose the better for your specific case.

1- Deleting the keyword "var" in cal function:

Doing that, the totalPay variable will have global scope. So, you will be able to manipulate it in another functions.

function cal(salary, additionalSalary) {
    var salary;
    var additionalSalary;
    totalPay = salary + additionalSalary;
}

function 401k() {
    var specialAccount = totalPay * 0.37;
    var normalAccount = totalPay - specialAccount;
}

Take in count that for use totalPay in function 401k, you need first to call cal function

2- Implement hoisting

Just declare your variable in the beginning of the scope without make an assignation. Doing that, you're declaring a global var with global scope, so it can be called in functions scopes

var totalPay;

Then, you can specify your functions as belong

function cal(salary, additionalSalary) {
    var salary;
    var additionalSalary;
    totalPay = salary + additionalSalary;
}

function 401k() {
    var specialAccount = totalPay * 0.37;
    var normalAccount = totalPay - specialAccount;
}

Take in count, that the totalPay var is not declared in cal function, just asigned a value and for use totalPay in function 401k, you need first to call cal function because if not it will be undefined.

3- Keep vars localy

Is a good practice to keep the managment of the vars localy, because with globals is easy to fail in the managment of values, lose the trazability of the changes and so on.

Any case, to implement this option you just need to return the totalPay var in cal function and after that, call cal function into 401k function:

function cal(salary, additionalSalary) {
    var totalPay = salary + additionalSalary;
    return totalPay;
}

function 401k() {
    var totalPay = cal (salary, additionalSalary) ;
    var specialAccount = totalPay * 0.37;
    var normalAccount = totalPay - specialAccount;
}

My recommendation is to use the option 3, but the better solution is the one who adjust for your specific situation, so evaluate it and choose one.

Upvotes: 1

Akif Hussain
Akif Hussain

Reputation: 365

You just need to return the totalPay ,like this, from the function cal(salary, additionalSalary) :

function cal(salary, additionalSalary) {
  let totalPay = salary + additionalSalary;
  return totalPay;
}

function 401k() {
  const totalPay = cal(salary, additionalSalary);
  const specialAccount = totalPay * 0.37;
  const normalAccount = totalPay - specialAccount;
}

OR

You can create a separate module where your cal() function is written and you export the globalValriable from that example.js file like this :

module.exports.cal = function (totalPay) { 
  console.log(totalPay);
};

AND

get it in the main file where needed :

var totalPay = require('respectiveDir'/thatModule);

Upvotes: 2

Yosvel Quintero
Yosvel Quintero

Reputation: 19080

You can use closures and immediately invoked function expressions to handle let totalPay variable within your salary calculation:

Code:

const Salary = (() => {
  let totalPay
  const cal = (salary, additionalSalary) => {
    totalPay = salary + additionalSalary
  }
  const k401 = () => {
    const specialAccount = totalPay * 0.37
    const normalAccount = totalPay - specialAccount
    return normalAccount
  }
  return {
    cal: cal,
    k401: k401
  }
})()

// Use it like this
Salary.cal(5000, 250)
const k401 = Salary.k401()

console.log('k401:', k401)

Upvotes: 3

Related Questions