Javascript全局变量生命周期 [英] Javascript global variable life time

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

问题描述

(我是Javascript初学者)

(I am Javascript Beginner)

虽然我正在学习Javascript全局变量的生命周期,但它说:

While I am learning Javascript global variable's life time, it say:

全局变量的生命周期在声明时开始,
在页面关闭时结束。

The lifetime of Global variables starts when they are declared, and ends when the page is closed.

我读过Javascript会将全局变量存储到window对象中吗?什么时候会被摧毁?我在网络浏览器中关闭标签后?

I read that Javascript will store the global variable into window object? And when will it be destroyed? After I close the tab in web browser?

例如:
如果abc.com/page1.html创建一个全局变量,那么在同一个标​​签中,我导航到abc.com/page2.html全局变量仍然存在吗?如果我导航到同一选项卡中的另一个域,例如another.com/page1.html怎么样?

For example: If abc.com/page1.html create a global variable, after that in the same tab, I navigate to abc.com/page2.html will the global variable still there? How about if I navigate to another domain in the same tab, for example, another.com/page1.html?

推荐答案

一次窗口被卸载所有的JavaScript变量都丢失了,假设你从第1页移到第2页,在窗口上留下你的变量丢失,如果它在同一个域或跨域上同样适用

Once window is unloaded all your JavaScript variables are lost, let's say you move from page 1 to page 2, on window leaving your variables are lost, same applies if it's on same domain or cross domain

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

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