Closed Emnalyeriar closed 7 years ago
Could I get a response to my question? That would be great :)
@Emnalyeriar I'll try to explain.
tl;dr You should use const
by default. let
should be used if value of a variable could be changed, for example for index in for
loop.
With the const statement you can add properties to object because of how const is implemented.
const
restricts changes of variable value. In case of objects, value will be reference to object, not object itself. So by making variable const
we are just protecting ourselves from accidental changes of reference.
The object is not immutable
variable is. That's the point.
p.s. I prefer this code:
export default function userFactory() {
const user = {};
return {
getUser() {
return user;
}
isSignedIn() {
return user.isSignedIn;
}
};
}
This makes sense, thank you for this explanation :)
With the const statement you can add properties to object because of how const is implemented. The object is not immutable so you can do that but it seems a bit illogical for me to expand an object that is declared as constant. Wouldn't a simple var/let be better here? Example code take from this starter: