I try to understand why the variable 'Permissions' will be typeof 'function' when Permissions declared with var but typeof 'object' when declared with let (which is the excepted behavior)
With 'var' - Permissions is typeof 'function'
var Permissions;
(function (Permissions) {
Permissions[Permissions["ADMIN"] = 0] = "ADMIN";
Permissions[Permissions["READ_ONLY"] = 1] = "READ_ONLY";
})(Permissions || (Permissions = {}));
console.log(typeof Permissions);
With 'let' - Permissions is typeof 'object'
let Permissions;
(function (Permissions) {
Permissions[Permissions["ADMIN"] = 0] = "ADMIN";
Permissions[Permissions["READ_ONLY"] = 1] = "READ_ONLY";
})(Permissions || (Permissions = {}));
console.log(typeof Permissions);
I excepted both scenario to be 'object'. why using 'var' Permissions is typeof 'function'?
Declaring a variable with
varin a global scope makes it a property ofwindow. Since you haven't initialized it, it's actually pointing to the existingwindow.Permissionsproperty, which is a constructor ofnavigator.permissions.letworks differently and declares a separate variable in the current scope:If you put your
varinto a function scope, it would work likelet:Also works as
letin a module scope:With this and other many problems inherited to
varI treatvaras legacy and dangerous and prohibit it with ESLint.