Javascript "classes" declared with the class keyword don't seem to appear in the window object. I'm trying to find a class object by name, e.g. window['A']
.
<html><head><script type='text/javascript'>
function A() {}
console.log(A) // outputs function A
console.log(window.A) // same
console.log(window['A']) // same
class B {}
console.log(B) // outputs class/function B
console.log(window.B) // undefined
console.log(window['B']) // undefined
</script></head></html>
I'm getting this behavior both in Chrome 52.0.2743.116 and Firefox Developer 50.0a2 (2016-08-27).
Looking at the MDN article on the class keyword, it mentions that such declarations aren't "hoisted" like function declarations, but that seems to be a different issue. I've looked through the specs but as far as I can tell this should behave like a function. ("7. Let env be the running execution context’s LexicalEnvironment. 8. Let status be InitializeBoundName(className, value, env)." seem to be the relevant points)
Does anyone know if this is a bug or I've missed something and this is by design? If so, is there another way of getting a class by it's declared name?