The name
is a variable of window
. Once you change the value, it will always have this value until you close the window.
When opening the window again, the value of name
is null. When you set a value to it, this variable will always have that value while the window is open.
name = "nome2";
console.log(window.name);
After setting a value to name
, give a refresh on the page and run the code below, without making any assignment to name
will have the result:
<script>
// name = "nome2" comentado
console.log(name); // imprime "nome2" no console
</script>
In case you want to return the name
of its object and not of the variable of the window
, would have to do so by adding a this
:
const obj = {
name : 'nome1',
age : 21,
funcaoObj () {
return this.name
}
}
name = 'name2'
console.log(obj.funcaoObj())
then I must assume that the variables within a function are "hunted" within scopes ?
– Natan Teodoro
It’s something called Scope Activation Registry, a concept of programming language and compilers.
– Sveen