'name'是如何成为全局变量的?

时间:2021-08-01 23:44:40

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'.

好的,所以我不确定这是否只是与Chromes开发人员工具隔离,但我正在使用我正在处理的一些代码并且我意识到在全局名称空间中创建了一个本地的变量'名称'。

'name'是如何成为全局变量的?

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.

我删除了该错误然后打开了Chrome工具以确保错误已得到修复。令我惊讶的是,我在控制台中键入“name”,并返回一个空字符串。在仔细查看我的代码之后,我意识到这不是由我造成的,所以我会去google.com再试一次。令我惊讶的是,'name'是一个全局变量。环顾四周后,看起来在使用Dev工具时几乎无处不在。

My question is why?

我的问题是为什么?

2 个解决方案

#1


6  

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

name是窗口对象的属性,与窗口的所有其他成员一样,它可以全局访问。

#2


2  

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

name等效于window.name,通常用于在创建窗口后修改窗口名称。

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

#1


6  

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

name是窗口对象的属性,与窗口的所有其他成员一样,它可以全局访问。

#2


2  

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

name等效于window.name,通常用于在创建窗口后修改窗口名称。

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