scope-chains-closures icon indicating copy to clipboard operation
scope-chains-closures copied to clipboard

Scoping vs Scopes suggestion

Open milosh86 opened this issue 10 years ago • 6 comments

Hi,

just a comment regarding scopes and scoping. I find some points in workshop to be little confusing, so I'll try to articulate it in couple of sentences.

  1. there are Lexical(static) and Dynamic Scoping
  2. there are Block, Function, Modul...Scopes

Scoping is related to how the scope chain is created. In Lexical or Static Scoping, "parent" scope of some "child" scope is determined by the place in source code where it is defined. So, the scope chain is created at the place where the certain scope (function/block) is defined. On the other hand, Lexical Scoping is contrasted by Dynamic Scoping, where scope chain is created dynamically in the place where the function is called (http://c2.com/cgi/wiki?DynamicScoping). All popular programming languages that I am aware of are Lexically Scoped.

Now, scope itself could be defined by block, function, module, etc.

So, I think it is not correct to say that variable created by var is lexically scoped and variable created with let – block scoped. Lexical scoping cannot be contrasted with block scope. var creates function scoped variables and let creates block scoped variables. Variables created with let are still Lexically Scoped, because its parent scope is determined the same way as for variables created with var, by the place where it is defined.

milosh86 avatar Jun 26 '15 12:06 milosh86

Interesting points!

var creates function scoped variables and let creates block scoped variables. Variables created with let are still Lexically Scoped, because its parent scope is determined the same way as for variables created with var, by the place where it is defined.

My understanding is: a variable is declared during the first pass the JS engine makes over the code. This process is called Hosting, and determines the shore of each variable. That act of determining the scope (hosting) is distinctly different to the type of scope determined (lexical or block).

Our to put another way; let & var do share the same mechanism of determining scope, but the results are different.

On the other hand, Lexical Scoping is contrasted by Dynamic Scoping

I'm inclined to leave details like that out of the workshop so it doesn't muddy the waters. But, if you find a good place to mention it where it can help improve the understanding, then I encourage you to open a PR!

So, I think it is not correct to say that variable created by var is lexically scoped and variable created with let – block scoped.

Is there a specific example in the workshop where you feel it could be reworded? Just do we're on the same page. On 26/06/2015 10:12 pm, "Milos Dzepina" [email protected] wrote:

Hi,

just a comment regarding scopes and scoping. I find some point in workshop to be little confusing, so I'll try to articulate it in couple of sentences.

  1. there are Lexical(static) and Dynamic Scoping
  2. there are Block, Function, Modul, etc Scopes

Scoping is related to how the scope chain is created. In Lexical or Static Scoping, "parent" scope of some "child" scope is determined by the place in source code where it is defined. So, the scope chain is created at the place where the certain scope (function/block) is defined. On the other hand, Lexical Scoping is contrasted by Dynamic Scoping, where scope chain is created dynamically in the place where the function is called ( http://c2.com/cgi/wiki?DynamicScoping). All popular programming languages that I am aware of are Lexically Scoped.

Now, scope itself could be defined by block, function, module, etc.

So, I think it is not correct to say that variable created by var is lexically scoped and variable created with let – block scoped. Lexical scoping cannot be contrasted with block scope. var creates function scoped variables and let creates block scoped variables. Variables created with let are still Lexically Scoped, because its parent scope is determined the same way as for variables created with var, by the place where it is defined.

— Reply to this email directly or view it on GitHub https://github.com/jesstelford/scope-chains-closures/issues/8.

jesstelford avatar Jun 26 '15 22:06 jesstelford

I agree with you. I would not either include anything about Dynamic Scoping into this workshop.

My point is just that Lexical Scoping should not be contrasted with Block Scopes. Lexical Scoping is about how names are resolved in nested scopes, no mather how and where are those names declared (var or let, current or parent scope). I think that every mention of Lexical Scoping should be swapped with "Function Scope" or "Function-level scope" in the workshop. For example, instead of "The main type of scope in Javascript is Lexical Scoping.", I would say "The most common type of scope in Javascript is Function Scope." or even better "In Javascript we have global, function and block scopes...explain each one".

Lexical Scoping mechanism might be explained in "Nested Scopes" chapter.

Here are some good resources, explaining above in much more details: http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/ https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Thanks, Milosh.

milosh86 avatar Jun 27 '15 12:06 milosh86

I think that every mention of Lexical Scoping should be swapped with "Function Scope" or "Function-level scope" in the workshop.

Oh, right, that makes way more sense - I completely miss-understood your original post, sorry.

I'll do some research (thanks for the links) and see what makes most sense to use.

Thanks for opening this issue.

jesstelford avatar Jun 28 '15 06:06 jesstelford

That's ok...obviously I didn't articulate it clearly enough ... We are now at the same page...great :)

Thanks, Milos.

milosh86 avatar Jun 28 '15 13:06 milosh86

I agree this should have some thought put into it, but we really need to think about this change. Currently, ''function scope javascript" has ~7.7m results but none very specific to the thing we're talking about. On the other hand, "lexical scope javascript" has only ~77k results, but they all talk about what we're talking about.. lexical variables. There have been other workshops that have knowingly used slightly incorrect terms because it makes it much easier for newcomers to understand/google.

SomeoneWeird avatar Jun 28 '15 13:06 SomeoneWeird

@SomeoneWeird Good point. Although I feel it's the responsibility of this workshop to get it right (especially as it's listed under "Core" on nodeschool.io), even if that means not being as google-able.

I guess the point to be made is that up until let & const existed, the distinction between function scope and lexical scope didn't matter. But now it does.

jesstelford avatar Jun 28 '15 23:06 jesstelford