In the browser, the window object has a dual role. It is not only an interface for accessing the browser window through JavaScript, but also a Global object specified by ECMAScript. This means that any object, variable and function defined in the web page has window as its Global object, so it has access to methods such as parseInt(). Variables and functions declared in the global scope will become window objects. Properties and methods
var age = 20; function sayAge() { //由于sayAge()存在于全局作用域中,因此this.age被映射到window.age,最终显示的仍然是正确的结果。 alert(this.age); } alert("window.age:" + window.age); sayAge(); window.sayAge();
Properties defined directly on the window object can be deleted through the delete operator
Js code
var i = 29; window.color = "red"; delete window.i;//抛出错误 delete window.color; alert(window.i);//29 alert(window.color);//undefined
Use the window object to access the declared variable oldValue
Js code
var newValue = oldValue;//报错 var newValue = window.oldValue;//不会报错,因为这是一次属性查询