2014-04-14 152 views
1

我试图创造出的情况下将使用的浏览器不支持班级名册定制element.prototype功能未定义“这个”

if(!Element.prototype.classList) { 
    Element.prototype.classList = {}; 

    Element.prototype.classList.contains = 
    function(class_name){ 
     console.log(this) 
    } 
} 

的执行console.log一个备份功能(这)返回“未定义”,我如何获取函数被调用的元素呢?

+0

你怎么称呼它? – Bergi

+0

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#classlist – Bergi

回答

0

这可能是IE8的console.log()以某种方式“打破”了结果。在IE8兼容模式(在IE11)控制台,这个正确显示[object Object] { }作为返回值,可膨胀的,以显示附功能:

Element.prototype.classList = {}; 
Element.prototype.classList.contains = function() { 
    return this; 
}; 
var n = document.createElement('div'); 
n.classList.contains(x); 

但是,包装,在一个console.log()返回值显示undefined。然而,只要让它“流过”到控制台,就可以按预期显示对象。

可能与古代IE怪癖有关,其中console不存在,除非开发人员工具打开?