Nested JavaScript functions and Non-static Inner Classes in Java

 

I am a Java programmer and understand the Java language concepts such as inner classes and scoping rules well. I have been learning JavaScript and trying to understand the scoping rules in JavaScript. Here are some of my observations about the similarities and differences between some aspects of nested JavaScript functions and Non-static inner classes.

 JavaScript
 Java
var global = 0;
function outerFunction() {
var outerLocal = 1;
function innerFunction() {
var innerLocal = 2;
// Can access outerLocal here
// Can access global here
}

return innerFunction;
}

 

 

 


outerFunction.staticVar = 10; 
// elsewhere
function callingFunction() {
// Get a reference to innerFunction()
var refToOuterDotInner = outerFunction();
refToOuterDotInner();

 

public class Outer() {
public static int staticField = 10;
private int outerLocal = 1;
public class Inner() {
private int innerLocal = 2;

private void innerMethod() {

int methodLocal = 4;
// Can access innerLocal here
// Can access outerLocal here
}
}

public Inner getInner() {
return new Inner();
}
}

// elsewhere
public void callingMethod() {
Outer outer = new Outer();
Inner innerInstance = outer.getInner();
// Invoke method of inner class
inner.innerMethod();
}

innerFunction() is analogous to --------->

The scope of innerFunction() maps to two concepts in Java world - the scopes of innerMethod() and the instance of Inner class.

The execution call stack of innerFunction() maps to the innerMethod() though.

 innerMethod() and instance of Inner class

I think this is the source of confusion for Java programmer. At least it was to me.


Three scopes when the innerFunction is executing:

  • Global
  • scope of outerFunction
  • scope of innerFunction - maps to last two scopes on Java side

Three scopes when innerMethod() is executing.

  • No equivalent in Java
  • Outer instance scope
  • Inner instance scope
  • innerMethod() local scope

outerLocal in outerFunction is analogous to --------->

outerLocal is private to outerLocal but can be accessed from innerFunction() .

outerLocal field of an instance of Outer class.

outerLocal is private to the instance of Outer class but can be accessed from innertMethod() .

In terms of scope level innerLocal is analogous to --------->methodLocal and innerLocal
no analogue
the Outer class instance can be accessed using Outer.this

call stack involves:

innerFunction()
callingFunction()

call stack:

innerMethod()
callingMethod()

outerFunciton.staticVar is analogous to --------->

Note the absence of  () after outerFunction.

static access to Outer.staticField

 

The scoping and access level rules of the inner functions allow for closures (a powerful concept) in JavaScript.

Do you have any insights to share?
 

Comments:

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today