The window object

  At the core of the BOM is the window object, which represents an instance of the browser. The window object serves a dual purpose in browsers, acting as the JavaScript interface to the browser window and the ECMAScript Global object. This means that every object, variable, and function defined in a web page uses windows as its Global object and has access to methods like parseInt().

The Global Scope

  Since the window object doubles as the ECMAScript Global object, all variables and functions declared globally become properties and methods of the window object. Consider this example:

1 var age = 29;
2 function sayAge(){
3   alert(this.age);
4 }
5
6 alert(window.age);
7 sayAge();
8 window.sayAge();

  Despite global variables becoming properties of the window object, there is a slight difference between defining a global variable and defining a property directly on window: global variables cannot be removed using the delete operator, while properties defined on window can. For example:

 1 var age = 29;
 2 window.color = "red";
 3
 4 // throws an error in IE < 9, returns false in all other browsers
 5 delete window.age;
 6
 7 // throws an error in IE < 9, returns true in all other browsers
 8 delete window.color;
 9
10 alert(window.age);          // 29
11 alert(window.color);        // undefined

  Properties of window that were added via var statements have their [[Configurable]] attribute set to false and so may not be removed via the delete operator.

  Another thing to keep in mind: attempting to access an undeclared variable throws an error, but it is possible to check for the existence of a potentially undeclared variable by looking on the window object. For example:

1 // this throws an error because oldValue is undeclared
2 var newValue = oldValue;
3
4 // this doesn‘t throw an error, because it‘s a property lookup
5 // newValue is set to undefined
6 var newValue = window.oldValue;

  Keeping this in mind, there are many objects in JavaScript that are considered to be global, such as location and navigator(both discussed later in the chapter), but are actually properties of the window object.

时间: 2024-08-29 10:15:29

The window object的相关文章

js中的window.open返回object的错误

系统中用javascript中的window.open后,页面返回了一个[object].因为系统的原因,必需使用href="javascript:window.open()"这样的格式.所以只能通过以下办法解决. 解决window.open后返回object的错误 <a href="javascript:void(window.open('','','width=200,height=200'))">window.open()</a> 只在

Document Object Model (DOM) Level 3 Events Specification

Document Object Model (DOM) Level 3 Events Specification W3C Working Draft 25 September 2014 This version: http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/ Latest published version: http://www.w3.org/TR/DOM-Level-3-Events/ Latest editor's dr

小程序基础03:pages与window配置

1.pages pages接收一个数组,每一项都是字符串来指定小程序的每一个页面,每一项代表对应页面的[路径+文件名.数组的第一项代表小程序的初始页面,小程序新增或者减少页面,都需要对pages进行修改. 文件名不需要写文件后缀,因为框架会自动去寻找路径 .json .js .wxml .wxss的四个文件进行整合. 实例: //pages: String Array 设置页面路径 "pages":[ "pages/index/index", "pages

VBA学习笔记(8)-Application Object Members

Application Object Members Represents an instance of Microsoft Office Visio. An external program typically creates or retrieves an Application object before it can retrieve other Visio objects from that instance. Use the Microsoft Visual Basic Create

【JavaScript】Object.prototype.toString.call()进行类型判断

权声明:本文为博主原创文章,未经博主允许不得转载. [javascript] view plain copy print? op = Object.prototype, ostring = op.toString, ... function isFunction(it) { return ostring.call(it) === '[object Function]'; } function isArray(it) { return ostring.call(it) === '[object A

window.navigate 与 window.location.href 的使用区别介绍

window.navigate(sURL)方法是针对IE的,不适用于FF,在HTML DOM Window Object中,根本没有列出window.navigate方法. 要在javascript中导航,不是调用window对象的某个方法,而是设置它的location.href属性,location属性是每个浏览器都支持的. 比如:<span onclick=”javascript:window.location.href=’#top’”>top</span>

window.location 小结)

其实在网上都能找到,我只是总结一下,方便自己查找使用 示例URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性     含义  值 protocol 协议  "http:" hostname 服务器的名字 "b.a.com" port 端口 "88" pathname URL中主机名后的部分 "/index.php" search "?&qu

Window Relationships and Frames

If a page contains frames, each frame has its own window object and is stored in the frames collection. Within the frames collection, the window objects are indexed both by number and by the name of the frame. The top object always points to the very

onload事件和window,document,body的研究

今天在工作中用到了onload事件,发现了一些有趣的事情,比如一般来说,如果我们需要给一个DOM结构绑定一个事件,我们一般会采用如下方法(以Window对象为例): [现象] window.onload = function() { console.log('<span style="font-family: Arial, Helvetica, sans-serif;">window.onload</span><span style="font-