The window object of the BOM element

In the browser, the window object has dual roles, it is not only an interface for accessing the browser window through JavaScript, but also the Global object specified by ECMAScript. This means that any object, variable and function defined in the web page uses window as its Global object, so it has access to methods such as parseInt()

 

Variables and functions declared in the global scope will become properties and methods of the window object

var age = 20;

function sayAge() {
    //Because sayAge() exists in the global scope, this.age is mapped to window.age, and the final display is still the correct result.
    alert(this.age);
}

alert("window.age:" + window.age);
myAge();
window.sayAge();

 

Properties defined directly on the window object can be deleted with the delete operator

var i = 29;
window.color = "red";

delete window.i;// throws an error
delete window.color;

alert(window.i);//29
alert(window.color);//undefined

 

Use the window object to access the declared variable oldValue

var newValue = oldValue;//Error
var newValue = window.oldValue;//No error will be reported because this is a property query

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327099423&siteId=291194637