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); //
  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.

The window object的更多相关文章

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

    系统中用javascript中的window.open后,页面返回了一个[object].因为系统的原因,必需使用href="javascript:window.open()"这样 ...

  2. 解决window.opener.obj instanceof Object会输出false的问题

    在a.html页面中: window.obj = {name: "jinbang"} 在a.html页面中打开新窗口b.html页面: console.log(window.ope ...

  3. window.open和window.location.href的几种用法

    windows.open("URL","窗口名称","窗口外观设定"); <A href="javascript:windo ...

  4. JavaScript Window 对象

    < JavaScript Window Object > && < IE check > JavaScript Window Object Window.loa ...

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

    window.navigate(sURL)方法是针对IE的,不适用于FF,在HTML DOM Window Object中,根本没有列出window.navigate方法. 要在javascript中 ...

  6. window.open()&&window.showmodaldialog()

    open 打开一个新窗口,并装载URL指定的文档,或装载一个空白文档,如果没提供URL的话. 适用于 窗口 语法 window = object.open([URL[,name[,features[, ...

  7. window.location 小结)

    其实在网上都能找到,我只是总结一下,方便自己查找使用 示例URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性     含义 ...

  8. document.onclick vs window.onclick

    The JavaScript Window object is the highest level JavaScript object which corresponds to the web bro ...

  9. android应用开发之Window,View和WindowManager .

    ViewManager  vm = a.getWindowManager(); vm.add(view,l); window :一个抽象的窗口基类,控制顶层窗口的外观和行为.作为顶层窗口,可控制窗口背 ...

随机推荐

  1. jq事件操作汇总

    bind()        向匹配元素附加一个或更多事件处理器blur( )        触发.或将函数绑定到指定元素的 blur 事件change()        触发.或将函数绑定到指定元素的 ...

  2. C++中的变量属性小结

    其实在C++中,一个变量除了数据类型以外,还有3种属性: (1)存储类别:C++中允许使用auto,static,register,extern 4种存储类别. (2)作用域:指在程序中可以使用该变量 ...

  3. jQuery和Prototype的兼容性和冲突的多种解决方法

    有两种情况: 1.先加载Prototype,再加载jQuery. 2.先加载jQuery,再加载Prototype. 针对情况1:先加载Prototype,再加载jQuery.方法一:jQuery 库 ...

  4. Java 实现《编译原理》简单词法分析功能 - 程序解析

    Java 实现<编译原理>简单词法分析功能 - 程序解析 简易词法分析功能 要求及功能 (1)读取一个 txt 程序文件(最后的 # 作为结束标志,不可省去) { int a, b; a ...

  5. 清北学堂dp图论营游记day3

    .状态压缩dp: 对于这个我们引入二进制状态压缩,因为任何一个数都可以二进制表示,而其二进制表示上每一位都可以表示当前位置是否有元素,这就构成了状态压缩. 对于这个题,上下行&一下就行. 状压 ...

  6. 多线程-生产者消费者(BlockingQueue实现)

    三.采用BlockingQueue实现 BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具. BlockingQueue有四个具体的实现类,根据不同需 ...

  7. dede 调取二级三级菜单栏目

    {dede:channelartlist typeid='} <div class="cate-item"> <div class="cate-item ...

  8. node的安装和配置教程

    node,除了做数据服务处理,还是各大框架的环境依赖,作为前端开发人员,node是必不可少的.好了,接下来直接开始. 一.根据自己的安装环境(即你的电脑系统版本)下载对应安装包,官网地址:https: ...

  9. MHA配置

    1,背景 MHA的目的在于维持MySQL Replication中Master库的高可用性,其最大特点是可以修复多个Slave之间的差异日志,最终使所有Slave保持数据一致,然后从中选择一个充当新的 ...

  10. JAVA学长

    https://www.cnblogs.com/chenmingjun/p/9697371.html