Inner function variable can access outer function variable, it is possible or not in java script.I am confusing for the closures.
function a(){
var x=10;
console.log(x,y);
return function b(){
var y=20;
console.log(x,y);
}
}
Inner function variable can access outer function variable, it is possible or not in java script.I am confusing for the closures.
function a(){
var x=10;
console.log(x,y);
return function b(){
var y=20;
console.log(x,y);
}
}
Yes, inner function variables can access outer function variables, BUT NOT THE OTHER WAY AROUND. Very good clarifying question. Below is a simple example of a function accessing a variable in an encompassing scope.
var name = 'John';
var Person = function(){
this.name = name;
}
var guy = new Person();
guy.name //John
function a(){
var x=10;
console.log(x);
return function b(){
var y=20;
console.log(x,y);
}
}
var x = a();
console.log('x defined')
x();
Try running this to see the sequence of interpretation in javascript