2

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.

dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • What's the hack you're working on, if I may ask? – aeskr Jan 14 '12 at 21:08
  • @AdleyEskridge--basically i'm trying to alter the map between xml and math graphics mathjax uses. Do you know if that kind of map exists? I've been searching through the documentation but cant find anything. – dopatraman Jan 15 '12 at 04:08
  • It looks like you might be viewing the minified source code rather than the original source (which uses more meaningful names than `d` and `b`). Try looking at `MathJax/unpacked/MathJax.js` instead of just `MathJax/MathJax.js` – Davide Cervone Jan 15 '12 at 19:10

1 Answers1

1

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);
Community
  • 1
  • 1
aeskr
  • 3,436
  • 1
  • 15
  • 10
  • @AdleyEskridge--scratch that. I tried using `alert(window.name)` and 'result' was returned. Can you explain what happened please? – dopatraman Jan 14 '12 at 21:26
  • 1
    @codeninja, did you try the above snippet on JSFiddle? Keep in mind that `window.name` is _not_ the same as `window[name]`; it's the same as `window['name']`. If you use `window[name]`, the result will be the value of the property whose name is equal to the _value_ of the `name` variable. I'm going to change the parameter name in my answer to prevent any confusion. – aeskr Jan 14 '12 at 21:29
  • @codeninja `window.name` returns the name of the current window. If the window is in an `iframe`, the name of the `iframe` is returned. On JSFiddle, the name of the iframe where results are displayed is "result", which is why you received that alert message. – aeskr Jan 14 '12 at 21:35