JavaScript assigns values to undeclared variables

If you assign a value to a variable that has not been declared, the variable will automatically be used as an attribute of the window.
This statement:

carname="Volvo";

An attribute carname of window will be declared. Global variables created by assigning values
to undeclared variables in non-strict mode are configurable properties of global objects and can be deleted .

var var1 = 1; // 不可配置全局属性
var2 = 2; // 没有使用 var 声明,可配置全局属性

console.log(this.var1); // 1
console.log(window.var1); // 1
console.log(window.var2); // 2

delete var1; // false 无法删除
console.log(var1); //1

delete var2; 
console.log(delete var2); // true
console.log(var2); // 已经删除 报错变量未定义

Guess you like

Origin blog.csdn.net/Serena_tz/article/details/114084457