The problem arises from the fact that after I assign a value to name, the value remains unchanged no matter how I refresh the page, which makes me a little suspicious of the life cycle of the js variable! I also encountered it by chance. In the previous test code, name was not defined and no value was assigned at the beginning, but the typeof name was string. Later, I printed out the window object and found that the object has the attribute name and it is an empty string, so it is easy to understand. But why after assignment, this value is not initialized to an empty string but always exists?
<!DOCTYPE html>
<html>
<head>
<title>web</title>
<meta charset="utf-8">
</head>
<body>
<button id="test">赋值</button></br>
<button id="show">显示</button></br>
<script>
//console.log(typeof name);
var name;
document.getElementById('test').onclick = function() {
name = 'hello word';
}
document.getElementById('show').onclick = function() {
alert(name);
}
//console.log(window);
</script>
</body>
</html>
When you assign name, name='hello word'; it is actually window.name='hellow world'; then when you look at the definition of window.name, it is Gets/sets the name of the window.
My understanding At this time, you set a name for this window, so no matter how you refresh it, the window is not closed, and the name is still there; this is not a life cycle problem of js variables
The problem is that if you refresh the page, it will be a new running context, so everything returns to the initial state. The value you assign to it is already in the previous running context. How can it be in the newly refreshed context? play a role?
Wow, this is the first time I have seen such a situation. If I put it in onload, this situation will not happen. I will wait for the guidance of the experts.
I just tried it, and it will indeed assign the value to the window object as an attribute, and it will not be erased when refreshing with F5
The following is an introduction to the window object. There is a sentence in it: All JavaScript global objects, functions and variables automatically become members of the window object.
Window Object
All browsers support the window object. It represents the browser window.
All JavaScript global objects, functions and variables automatically become members of the window object.
Global variables are properties of the window object.
Global functions are methods of the window object.
For example, if we define a global variable name1, it can also be found in the window object.
window object, which represents the browser window. Therefore, as long as the window is not closed, even if F5 is refreshed, the value inside (the properties of the winow object itself when needed) will always exist.
It suddenly occurred to me that window.name can also be used to achieve cross-domain data transmission.
I guess it may have something to do with the automatic conversion type of js and the
Thetypeof
pit, because the code is changed to:log output is still
string
, so the problem of divine assignment can be ruled out.I checked MDN just now and found that the
window
本身有个name
attribute (portal)...MDN didn’t provide much information, so I went to refer to the relevant instructions in Mr. Xia Ruan’s "JavaScript Standard Reference Tutorial":When the name variable has not been defined, the top-level one is directly accessed.
window.name
This is a string... maybe this is what it means?