Another similar gotcha is that the global-scoped `name` variable must be a string. See https://developer.mozilla.org/en-US/docs/Web/API/Window/name for details. var name = true; typeof name; // "string", not "boolean" Luckily, this is not true within ES modules which you probably use most of the time anymway.
var _value = "test value";
Object.defineProperty(window, "testName", {
get: () => _value,
set: (value) => { _value = String(value) },
});
var testName = {};
// prints [object Object] string
console.log(testName, typeof testName);
var name = {};
// prints [object Object] string
console.log(name, typeof name);
the `var` doesn't create a new property since the getter and setter already exist.Other properties have the same behavior, for example `status`.
Note: there's also LegacyUnforgeable which has similar behavior: https://webidl.spec.whatwg.org/#LegacyUnforgeable
Even if you're not using modules, using an IIFE avoids all this by making your variables local instead of having them define/update properties on the global.