Skip to content
LYF edited this page Aug 9, 2016 · 9 revisions

1. window比较

   window == window.window // true

   window == window.self // true

   window === window.window // false

   window === window.self // false

与标准的区别:在标准下,4个test都返回true

2. toString的返回结果

12个build-in对象:Global,Object,Number,Boolean,String,Array,Function,Date,RegExp,Math,JSON,Error

测试:

10个build-in(native object)对象:

Number,Boolean,String,Array,Function,Date,RegExp,Math,JSON,Error

4个host对象:

window,Arguments,document,document.body

2个基本类型:

null,undefined

var toString = Object.prototype.toString;

toString.call(1); // [object Number]
toString.call(NaN); // [object Number]
toString.call(new Number(1)); // [object Number]

toString.call(false); // [object Boolean]
toString.call(new Boolean(true)); // [object Boolean]

toString.call('abc'); // [object String]
toString.call(new String('abc')); // [object String]

toString.call(new Array()); // [object Array]
toString.call([]); // [object Array]

toString.call(/\d/); // [object RegExp]
toString.call(new RegExp("a")); // [object RegExp];

toString.call(Math); // [object Math]
toString.call(JSON); // [object JSON]
toString.call(function(){}); // [object Function]
toString.call(new Error); // [object Error]
toString.call(new Date);// [object Date]


toString.call(window); // [onject Object];
toString.call(arguments); // [onject Object];
toString.call(document); // [onject Object];
toString.call(document.body); // [onject Object];


toString.call(null); // [onject Object];
toString.call(undefined); // [onject Object];

结论:10个build-in对象返回结果与标准浏览器一致,但是host对象在IE8-下统一返回[object Object],同时null,undefined也返回[object Object]

3. window == document 和 document == windpw

IE678下有一个神奇的特性:

window == document 为true

document == window 竟然为false

所以可以利用这个神奇特性做一些事情,例如判断浏览器是不是IE8-

var isIE8Low = function(){
  return window == document && document != window;
}

4. IE8无法删除window下的属性

// Known issue: Will throw 'Uncaught ReferenceError: callback_*** is not defined' error if request timeout
function clearFunction(functionName) {
    // IE8 throws an exception when you try to delete a property on window
    // http://stackoverflow.com/a/1824228/751089
    try {
        delete window[functionName];
    } catch (e) {
        window[functionName] = undefined;
    }
}
Clone this wiki locally