I am a beginner and I am learning JavaScript. I am tying to get a private variable of a function which is within a object. How can I call the private variable totalSalary
outside the object and get the value of the variable?
Here is my JavaScript code:
const info = {
firstName: "Richard",
lastName: "Benson",
mainSalary: 250000,
monthlySell: 1000000,
finalSalary: function(bonusParcent) {
const sellsBonus = this.monthlySell * bonusParcent;
const totalSalary = this.mainSalary + sellsBonus; // This variable
return sellsBonus;
}
}
console.log("Full name: " + info.firstName + " " + info.lastName);
console.log("Main salary: $", info.mainSalary);
console.log("Sells bonus: $", info.finalSalary(0.05));
console.log("Total salary: $", info.finalSalary().totalSalary); // Want to call form here
How can I call the totalSalary
the variable?