javascript - Custom element.prototype function has undefined "this" -


i'm trying create function used in case browser doesn't support classlist

if(!element.prototype.classlist)    {   element.prototype.classlist = {};    element.prototype.classlist.contains =     function(class_name){         console.log(this)     } } 

the console.log(this) returns "undefined", how element on function called then?

it may ie8's console.log() "breaking" result somehow. in console in ie8 compatibility mode (in ie11), correctly shows [object object] { } return value, expandable show attached contains() function:

element.prototype.classlist = {}; element.prototype.classlist.contains = function() {   return this; }; var n = document.createelement('div'); n.classlist.contains(x); 

but, wrapping return value in console.log() shows undefined. letting "flow through" console, however, shows object expected.

possibly related ancient ie quirk, wherein console doesn't exist unless developer tools opened?


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -