PDA

View Full Version : Newbie: First 6 lines of ExtJS implementation



ferjjf
11 Sep 2011, 6:01 AM
Hello everyone, I was snooping around on the ExtJS implementation and I couldn't figure out exactly what this initial piece of code does:


var global = this,
objectPrototype = Object.prototype,
toString = objectPrototype.toString,
enumerables = true,
enumerablesTest = { toString: 1 },
i;


Why is it comma-separated? Is 'i' already defined somewhere? What would be the final value for 'global'?
(I already tried using the console debugger bundled with Chrome but couldn't figure it out)

Thanks in advance and I know... this is a newbie question LOL!

skirtle
11 Sep 2011, 7:22 AM
Defining variables like this is quite common. The reason is minification. A simple variable name can be minified to one character, e.g. the single letter a. Keywords like var and this cannot be minified. Likewise, standard names of the language like window, toString and Object can't be minified.

Using comma-separation to specify multiple variables at once reduces the number of times var appears. Arguably minifiers should improve to do this automatically but currently they don't.

global will be equal to the global window object. Capturing it in this way not only makes minification easier but also protects against meltdown if someone accidentally assigns another value to the window variable (which happens quite often when people aren't careful to scope their variables properly).