Questions about whether variables are declared in javascript.

  node.js, question

The code I wrote in my own webstorm is javascript or node.js. I am not very clear, and I do not know about this There is No Sexual Rapport. The following is my code:

var fullname ='John Doe';
 var obj ={
 fullname:'Colin Ihrig',
 prop:{
 fullname:'Aurelio De Rosa',
 getFullname:function(){
 return this.fullname;
 bracket
 bracket
 };
 //console.log(obj.prop.getFullname());
 var test = obj.prop.getFullname;
 console.log(test());
 
 The output is then undefined;

Then if I remove var, it’s like this.

fullname ='John Doe';
 var obj ={
 fullname:'Colin Ihrig',
 prop:{
 fullname:'Aurelio De Rosa',
 getFullname:function(){
 return this.fullname;
 bracket
 bracket
 };
 //console.log(obj.prop.getFullname());
 var test = obj.prop.getFullname;
 console.log(test());
 Then the output is john Doe.
 Ask the great god for an answer.

The picture is as follows:
图片描述

图片描述

Under global scope, use thevar a = xxEquivalent towindow.a = xxSimilarly:

var test = obj.prop.getFullname;
 //Equivalent to
 window.test = obj.prop.getFullname;

In such a methodthispoint towindow, the final call iswindow.fullname.

The two pieces of code you wrote in the browser environment, the execution result is the same, both arejohn Doe.
In the node environment,varIn the global definition is limited to the current module, removevalexactlyglobalVariable.