The name variable in JavaScript seems a bit difficult to understand.
怪我咯
怪我咯 2017-05-19 10:45:19
0
5
498

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>
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(5)
某草草

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 typeofpit, because the code is changed to:

console.log(name);
console.log(typeof name);

var name;

document.getElementById('test').onclick = function () {
    name = 28;
}

document.getElementById('show').onclick = function () {
    alert(name);
}

console.log(window);
The

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本身有个nameattribute (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.nameThis is a string... maybe this is what it means?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template