Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139982

Javascipt emulates private variables but javascript are already private because of scope?

$
0
0

I've been studying javascript just now and i'm currently in closures. It is said that closure emulates private variables. But aren't variables only accessible on their own scope making them private by default? Given this example

function manageSalary () {
    let salary = 0;
    function updateSalary(amount) {
        salary += amount;
    }
    return {
        raise: function(amount) {
            updateSalary(amount);
        },
        deduct: function(amount) {
            updateSalary(amount);
        },
        current: function () {return salary}
    }
};

How exactly does closure makes the salary variable private in this example? Isn't salary already private since you cant access it outside this manageSalary? What I understood from this example rather is that closure emulates private methods instead.


Viewing all articles
Browse latest Browse all 139982

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>