JavaScript全局变量的生命周期 [英] The lifetime of JavaScript global variables

查看:142
本文介绍了JavaScript全局变量的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在w3schools上阅读了以下内容,但我不理解第二部分的含义:

I read the following on w3schools and I don´t understand what it means by the second part:

[全局变量]仍可用于加载到同一窗口中的新页面.

[global variables] remain available to new pages loaded into the same window.

任何人都可以启发我吗?

Anyone can enlighten me ?

在网络浏览器中,关闭浏览器窗口(或选项卡)时,全局变量将被删除,但对于加载到同一窗口中的新页面仍然可用.

In a web browser, global variables are deleted when you close the browser window (or tab), but remain available to new pages loaded into the same window.

链接: https://www.w3schools.com/js/js_scope.asp

推荐答案

如果w3schools真的这么说 (天堂帮助我们,确实如此),那是完全错误的,可以通过一个简单的实验轻松证明.

If w3schools really says that (heaven help us, it does), it's just plain wrong, as can easily be proved with a simple experiment.

在加载网页时,将重新创建该页面上JavaScript的全局环境.在同一窗口中,前一页的全局变量对新页面不可用.显然,浏览器提供的其他一些东西(例如存储在Web存储中的东西)可能是全局变量,而不是全局变量.

When a web page loads, the global environment for the JavaScript on that page is created fresh. No globals from a previous page in that same window are available to the new page. Obviously, some other things provided by the browser (such as things stored in web storage) may be, but not global variables.

尽管随着时间的推移有所改善,但不幸的是,这并不是人们在w3schools上注意到的第一个简单的事实错误.我建议改用 MDN的文档.虽然MDN是由社区编辑的,因此有时偶尔会出现错误,但总的来说,它是相当不错的,并且一个用户添加的任何错误往往会很快被其他用户修复.

While it's improved a bit over time, unfortunately this isn't the first simple factual error people have noted on w3schools. I'd suggest using MDN's documentation instead. While MDN is community-edited and thus does sometimes have the occasional inaccuracy, over all it's quite good, and any inaccuracies one user adds tend to be fixed fairly quickly by others.

评论中,您问过:

什么是JavaScript官方文档页面?

What's the official JavaScript documentation page ?

JavaScript的唯一官方"文档是规范.同样,各种规范中也记录了诸如DOM之类的浏览器功能. WHAT-WG" HTML5标准"(比HTML还要多) HTML(显然)和其他各种浏览器功能.其他人都有自己的规格. MDN 的一大优点是它链接到规范.

The only "official" documentation for JavaScript is the specification. Likewise, browser features like the DOM are also documented in various specifications. The WHAT-WG "HTML5 standard" (which is about a lot more than HTML) also documents HTML (obviously) and a wide range of other browser features. Others are in their own specs. One of the great things about MDN is that it links to specs.

这篇关于JavaScript全局变量的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆