Scoping of variables in Javascript

Little bit of research on this and that in javascript

Scoping of variables in Javascript

Search for: Scoping of variables in Javascript

an article I ran into


var xyz = "Global";
xyz = "Global as well";
function f()
{
    var xyz = "local, overrides the global one";
    var abc = "local. Not seen outside of the function";
    abc = "Global again, but available only after f() is called once";
    for (....)
    {
       var blah = "No such thing as block level scope but only function level";
    }
}

Makes the variable scope walk up the chain and declare it globally....


//call f()
f();
//define f() later
function f() {}

//becomes
//define f() later
function f() {}

and

f();

alert(helloVariable);
var helloVariable = "xyz";

//becomes

var helloVariable;
alert(helloVariable);
helloVariable = "xyz";

ECMA script introduced block level scoping through let and const

Search for: ECMA script introduced block level scoping through let and const


function f()
{
  var abc; //private
  this.xyz; //public member available outside through new
}

function f()
{
  var abc; //private
  this.xyz; //public member available outside through new
  var function privatef() 
  {
     //private function
     //can use abc and xyz
  }
  this.publicfunc = function publicf() 
  {
     //public function
     //cannot see private varilabe abc????
  }
}

function f()
{
  var abc; //private
  this.xyz; //public
  this.that = this; //trozan horse

  this.publicfunc = function publicf() 
  {
     //public function
     //cannot see private varilabe abc????
     //Now it can
     console(that.abc);
  }
  
}

Crockford on private functions


Private variables
Private functions
Public variables
Public functions
Public functions that get privileged through the "that" variable