User2
User2

Reputation: 591

How did 'name' become a global variable?

Okay so I'm not sure if this is just isolated to Chromes developer tools, but I was toying around with some code I was working on and I realise that a variable, meant to be local, had been created in the global name space called 'name'.

enter image description here

I removed the bug then opened up Chrome tools to make sure the bug had been fixed. To my surprise I type 'name' into the console and an empty string is returned. After much looking through my code, I realise this isn't caused by me, so I head over to google.com and try again. To my surprise 'name' is a global variable there. After looking around, it appears there is a global variable almost everywhere when looking with Dev tools.

My question is why?

Upvotes: 3

Views: 283

Answers (2)

JpBaena13
JpBaena13

Reputation: 96

name is equivalent to window.name and is often used to modify the name of a window, after the window has been created.

var myWindow = window.open("","MsgWindow","width=200,height=100");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");

Upvotes: 3

p.s.w.g
p.s.w.g

Reputation: 149058

name is a property of the window object, and like all other members of window, it's accessible globally.

Upvotes: 7

Related Questions