`
dowhathowtodo
  • 浏览: 775357 次
文章分类
社区版块
存档分类
最新评论

window.undefined=window.undefined

 
阅读更多
在jquery中我们见到window.undefined=window.undefined的写法,今日又在ext中见到window["undefined"]=window["undefined"],对其写法非常不理解,将自身赋给自身有什么意义,在网上狂搜一番也没有明确的解释,后来看到ext注释写了这么一句:for old browsers,很明显是为了兼容老的浏览器,思虑半天,似乎有了些理解。

在较老的浏览器中,如IE5之前的浏览器,undefined并不是window对象的一个子对象,并不是一个已实现的系统保留字,而是代表一个未定义类型,除了直接赋值和typeof()之外,其它任何对undefined的操作都将导致异常。如果需要知道一个变量是否是undefined,只能采用typeof()的方法:如var v;if (typeof(v) == 'undefined') {// ...}。如果使用if(a==="undefined")则会报“undefined未定义”的错误。
因此为了兼容IE5及之前的浏览器,我们可以使用一些方法来解决这个问题。本文前面说到的就是其中一种方式。window.undefined=window.undefined;咋一看很难理解,写法有点bt,但理解一下就不觉得奇怪了,在较早的浏览器中因为window.undefined不存在所以会返回undefined,将此赋给等号前的window.undefined这样后面就可以直接使用if(a==="undefined")的判断方式了。在新版本的浏览器中window.undefined=undefined;因此不会造成什么负面影响。

除了使用window.undefined=window.undefined和window["undefined"]=window["undefined"]外,还有很多别的办法来实现对IE5及之前浏览器的兼容,如
var undefined = void null; //void函数永远返回undefined
var undefined = function(){}();
var undefined = void 0;
只要等号后的表达式返回undefined即可。
分享到:
评论

相关推荐

    jQuery基础

    // Will speed up references to window, and allows munging its name. window = this, // Will speed up references to undefined, and allows munging its name. undefined, // Map over jQuery in case of ...

    tweenjs.min.js文件

    if(a==Ticker.RAF_SYNCHED||a==Ticker.RAF){var b=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window....

    任意拖动的视频播放器

    _root.playlist == undefined ? playlist="http://yun.365.sh/s/TY2gc2.swf" : playlist=_root.playlist; // stage variables Stage.showMenu = false; Stage.scaleMode = "noScale"; stop(); // video Stream ...

    Select精美下拉框(漂亮)

    if ('propertychange'==window.event.type && 'selectedIndex'==window.event.propertyName) { var oSS = window.event.srcElement.SS; oSS.syncSelectedIndex(); if (null != oSS.select.onchange) oSS.select...

    simplewebrtc.js webrtc网页视频开发帮助工具

    arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; func._groupName = group; (this.callbacks[event] = this.callbacks[event] || []).push(func); return this; }; // Adds an `...

    js解析二维码llqrcode.js

    if (window.createObjectURL!=undefined) { // basic url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) ; } else if ...

    ajaxAnywhere框架概念和使用.doc

    AjaxAnywhere的类库及其用法 AjaxAnywhere使用一个名为aa.js的Javascript文件来处理客户端的全部Ajax操作,包括初始化...if (this.req == null || typeof this.req == "undefined") this.notSupported = true; }

    js的日历时间控件

    if(typeof Control=="undefined"){Control={}}Control.DatePicker=Class.create({initialize:function(element,options){this.element=$(element);if(dp=this.element.retrieve("datepicker")){dp.destroy()}this....

    newMap地图

    (function(a){a.N={VERSION:...return function(c){return c[b]=c[b]||"_newmap_id_"+ ++a,c[b]}}(),requestAnimFrame:function(){function a(a){window.setTimeout(a,1e3/60)}var b=window.requestAnimationFrame||

    js使用小技巧

    取变量类型 typeof($js_libpath) == "undefined" 下拉框 下拉框.options[索引] 下拉框.options.length 查找对象 document.getElementsByName("r1"); document.getElementById(id); 定时 timer=setInterval...

    新浪推荐浮动广告

    <br>var $dom={ parseInt : function(s) { if (s == null || s == '' || typeof(s)=='undefined') return 0; <br> return parseInt(s); }, getClientSize : function(n){ if ($...

    my97日历控件

    var Y=window,T={innerHTML:""},N="document",H="documentElement",C="getElementsByTagName",V,A,S,G,c,X=navigator.appName;if(X=="Microsoft Internet Explorer")S=true;else if(X=="Opera")c=true;else G=true;A...

    右下角弹出窗口 javascript

    部分代码如下: #rbbox{position:absolute;right:0;bottom:0;width:300px;height:0px;...if (o==undefined) o=document.getElementById("rbbox"); o.style.height=o.clientHeight+2+"px"; if (o.clie

    js上传图片并取色代码.zip

    url = window.webkitURL.createObjectURL(file) ; } return url ; } //十六进制转换器 let hexadecimal = function(num) { var r = parseInt(num).toString(16); if (r.length == 1) {...

    window.open被浏览器拦截后的自定义提示效果代码

    现在越来越多的浏览器有拦截弹出窗口的功能。... Gmail的“哎呀”算是经典,可是,前天心云... window.open=function(sURL,sName,sFeatures,bReplace){ if(sName==undefined){sName=”_blank”}; if(sFeatures==undefin

    js模式化窗口问题![window.dialogArguments]

    在火狐浏览器中弹出一个子窗口,子页面中是一个分页,点下一页后子页面会刷新,然后window.dialogArguments对象就丢失了,alert输出显示undefined [解决方法见第三项] 最近做网站的时候需要用到模式化窗口功能,也遇到了...

    javascript基于HTML5 canvas制作画箭头组件

    样例: ... if (window.mapleque==undefined) window.mapleque={}; if (window.mapleque.arrow!=undefined) return; /** * 组件对外接口 */ var proc={ /** * 接收canvas对象,并在此上画

    myjquery:jquery源码分析

    (function(window, undefined) { function jQuery(selector){ return new jQuery.fn.init(selector) } jQuery.fn = jQuery.prototype = { init: function () { } } jQuery.fn.init.prototype = jQuery....

    jQuery的初始化与对象构建之浅析

    将undefined 作为缺失的参数传递,防止了undefined 变量的污染; 3.可以看出$(…) 实际上返回的是jQuery.fn.init 对象的实例,随后将该对象的prototype 指向了jQuery.prototype (语句jQuery.fn.init.prototype = ...

    淘宝ua 2015-12-29版反混淆

    typeof p == 'undefined' && (p = 0), typeof w == 'undefined' && (w = 0); var y = window.document['body']['clientWidth'], b = window.document['body']['clientHeight'], M = window.window['screen']['...

Global site tag (gtag.js) - Google Analytics