name
is a window
variable. Once you change the value, it will always have this value until you close the window.
When you open the window again, the value of name
is null . When you set a value to it, this variable will always have this value while the window is open.
name = "nome2";
console.log(window.name);
After setting a value to name
, refreshing the page and executing the code below, without doing any assignment to name
will have the result:
<script>
// name = "nome2" comentado
console.log(name); // imprime "nome2" no console
</script>
Ifyouwanttoreturnthename
ofyourobjectandnotthevariableofwindow
,youwouldhavetodothisbyaddingathis
:
const obj = {
name : 'nome1',
age : 21,
funcaoObj () {
return this.name
}
}
name = 'name2'
console.log(obj.funcaoObj())