The following code works fine
// Define the closure
var rentPrice = function (initialRent) {
var rent = initialRent;
// Define private variables for
// the closure
return {
getRent: function () {
return console.log(rent);
},
incRent: function (amount) {
rent += amount;
console.log(rent);
},
decRent: function (amount) {
rent -= amount;
console.log(rent);
},
};
};
var Rent = rentPrice(8000);
// Access the private methods
Rent.incRent(2000);
Rent.decRent(1500);
Rent.decRent(1000);
Rent.incRent(2000);
Rent.getRent();
But if I change it to let or const it gives an error
VM1926:1 Uncaught SyntaxError: Identifier 'rentPrice' has already been declared
So if the code is changed to the following it gives the error
// Define the closure
let rentPrice = function (initialRent) {
let rent = initialRent;
// Define private variables for
// the closure
return {
getRent: function () {
return console.log(rent);
},
incRent: function (amount) {
rent += amount;
console.log(rent);
},
decRent: function (amount) {
rent -= amount;
console.log(rent);
},
};
};
let Rent = rentPrice(8000);
// Access the private methods
Rent.incRent(2000);
Rent.decRent(1500);
Rent.decRent(1000);
Rent.incRent(2000);
Rent.getRent();
Question :-
Why am I getting this error I am not redeclaring rentPrice
I am invoking it and storing it in a variable Rent
so why am I getting this error ?