2

I have the following doubt relating the exact meaning of the lexical scope concept in JavaScript.

So, from what I have understood it can be explained by:

void fun()
{
    int x = 5;

    void fun2()
    {
        printf("%d", x);
    }
}

showing that any inner level can access its outer levels variables.

So is it the concept of the lexical scope ? If yes why is it named lxical? what exactly means?

eddie
  • 1,252
  • 3
  • 15
  • 20
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Lexical scope is also called static scope (in contrast to dynamic scope). In dynamic scope a function can access variables depending on its runtime context, in static scope it can only access variables from the context where it is defined in the source code – devnull69 Aug 18 '16 at 13:15
  • 1
    Check this Question : http://stackoverflow.com/questions/1047454/what-is-lexical-scope – Nihar Sarkar Aug 18 '16 at 13:19
  • "Lexical scope" basically means "the scope as written, literally." That's opposed to scoping rules which may depend on factors other than *the layout of the source code.* – deceze Aug 18 '16 at 13:21

2 Answers2

1

Even if the code you have posted is not JavaScript, you are essentially right. A function () { … } creates a scope and »inner a inner scope« can access the variables from the »outer«

var a = 10, b = 20;

(function () {
    var b = 100;
    console.log(a); //10
    console.log(b); //100

    (function() {
        var c = 333;
    })();

    console.log(c) //undefined
})();

It is notable to say that the scope, a function is defined within, is saved with the function. This means:

function A (a) {
    var b = 100;

    return function (c) {
        console.log(a, b, c);
    }
}

var afx = A(10);
afx(666); //10, 100, 666;

However, Javascript does not provide blockscope. So:

for (var i = 0; i < 10; i++) { … }
console.log(i) //9

the variable is not »scoped« to the block.

BUT ES6 and the new keyword let, to define variables, change this. So:

for (let i = 0; i < 10; i++) { … }
console.log(i); // ReferenceError: i is not defined (using babel to transpile)

So in future versions of JS, also Blockscoping is possible like this:

{
    let i = '#';
    console.log(i); //#
}
console.log(i); // ReferenceError: i is not defined
philipp
  • 15,947
  • 15
  • 61
  • 106
0

lexical scope is independent from the execution context, which is the case with your example.

A dynamic scope would depend on the context where the function is executed:

this.word = 'hello';
function saySomething() {
  console.log(this.word);
}

saySomething(); // "hello"

var otherContext = {
    word : "bye"
}

saySomething.call(otherContext); // "bye"
François Richard
  • 6,817
  • 10
  • 43
  • 78
  • 1
    `this` is a bad demonstration of scoping rules, since it really acts more like a magic keyword, not something to do with scope. – deceze Aug 18 '16 at 13:23
  • not sure to follow you can you elaborate please ? – François Richard Aug 18 '16 at 13:25
  • `this` is something that's explicitly passed to the function (either the object you're calling it on or even more explicitly via `call`/`apply`). Scope is something which implicitly makes defined symbol names available within a function. IMO that's a bit of a difference and hence an insufficient example. It's not really possible to demonstrate dynamic scoping in JS because JS doesn't have dynamic scoping… – deceze Aug 18 '16 at 13:52
  • hmm ok thanks I guess I'll have to dig more to understand really this notion ^^ – François Richard Aug 18 '16 at 14:42
  • It is possible to mix up the 'this' keyword with scoping as it is sometimes dependent on lexical context but not always. It is affected by other factors notably whether the function is invoked by `call` or `apply` and whether the code is running in strict mode or not, while scope is a wider concept affecting what is available in which lexical contexts. Look at the examples in the original answer (that this is marked as duplicate of) for an explanation of scope. – ChrisM Apr 04 '18 at 10:09