I am studying and trying to understand how scope works in javascript. My main problem is that when I change the value of a variable using a function, it gives me not what I want. I expect to receive in console last scope, like in function visible_scope_1();. but as for the last function visible_scope_3(), I just wanted to do it a little differently, but I also got a failure. In general, you may not pay attention to the last function.
// Very well ! So and want ...
// only do with help at scope visible functions ...
function visible_scope_1() {
var message = "local scope";
message = "changed local scope";
message = "last scope";
return message;
}
// Wrong : show all resuls ...
function visible_scope_2 () {
var message = "local scope";
var a1 = function() {
console.log(message = "inside local scope");
var b2 = function () {
console.log(message = "deeper scope");
var c3 = function () {
// I want to get the result from this scope. Is it possible?
console.log(message = "last scope");
}; c3();
}; b2();
}; a1();
}
// Wrong : show undefined
function visible_scope_3 () {
message = "local scope";
var a1 = function() {
message = "inside local scope";
return message;
var b2 = function () {
message = "deeper scope";
return message;
var c3 = function () {
// i expect to show in console "last scope"
message = "last scope";
return message;
}; c3();
}; b2();
}; a1();
}