A lot of the time, I write JavaScript that runs with other scripts or that may contain other scripts. Sometimes these scripts might have changed the prototypes for the primitive objects that I might be using in my code.
Is there a way I can declare my primitive datatypes in JavaScript so that it will reset the prototype if it has been modified? Or so that my scripts would run in a separate scope where the prototype of the primitive is not modified?
// evil script code modify primative on prototype
Boolean.prototype.toString = function() {
return true;
}
let flag = false;
console.log(flag.toString());
// I would expect false to be printed, but because the prototype of the primative if overridden
// the primative value would be wrapped with the Boolean object
// and call the modified toString method and would output true.
Is there any way I can make sure my code is running in a separate scope, or any way I can declare my variables or reset the prototypes to avoid these types of issues?