Question
I'm trying to understand MathJax's api for a hack i'm writing. The first line of code is an anonymous function that has a window
array. What is this "window
array"? Here is the code:
(function (d) {
var b = window[d];
//...
})('MathJax')
Please help me make sense of this.
Solution
That isn't an array; it's just the window
object.
In JavaScript, there are two ways to access an object's properties: object.property
and object['property']
.
The first syntax only works when the property's name is a valid JavaScript identifier; the second works for any property name.
Here's a contrive demonstration that somewhat matches your code (try it on JSFiddle):
function lookThroughWindow(nameOfProperty) {
alert(window[nameOfProperty]);
}
var propertyName = 'location';
lookThroughWindow(propertyName);
// The above just does this:
alert(window.location);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow