I was having problems debugging my javascript code in Google Chrome browser the other day. I was not able to use the Chrome Developer Tools’ Console while stepping through a particular method (let’s say “myProblematicMethod”) in the javascript code, and was always getting the following error whenever I typed anything into the Console:

 

TypeError: Cannot read property '_commandLineAPI' of undefined

It was more confusing because I was able to use Console for the javascript methods preceding and succeeding the “myProblematicMethod”, i.e. the Console behaved normally while stepping through the caller that called the myProblematicMetod before and after the call. For this particular method only, anything typed into the Console always gave the “TypeError: Cannot read property ‘_commandLineAPI’ of undefined” error.

I tried re-starting Developer Tools and even the Chrome browser itself, but the error for this particular method persisted. Stumped, on close inspection and line-by-line step-through of the problematic method starting from its first line, I was able to point out the issue arising from the following line of code:

 

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; } var window = new Ext.Window({
title: integratorConfig.mailAttachmentWindowText,
width: 650,
height: 500,
closeAction: ‘hide’,
……//more code{/syntaxhighlighter}

Can you see what’s wrong above. No, c’mon try inspecting closely…

Still haven’t figured it out, well let me tell you. Did you notice we are assigning to the javascript’s native “window” variable here, Ouch!!

No wonder that subsequent debugging and usage of the Console from this line forward failed, we had inadverently overwritten the javascript’s ubiquitous and intrinsic “window” variable, and almost all actions in javascript are somehow tied to the current window object, right??

I was rather surprised that browsers do not consider it a violation of some sort trying to overwrite intrinsic javascript objects like “window” or “document”, and reject the operation raising an exception.

But in any case, do I need to tell you the solution (well you have guessed correctly, a variable rename was all it took to resolve the issue).