1

When I read YDKJS(You Dont Know JS) book in there getify says "First, Compiler declares a variable (if not previously declared in the current scope)" - but I don't understand how compiler declare a variable? Compiler only turn code to machine code and engine run our code. My question is Engine Declare variable or Compiler declare our variable?

Murad Sofiyev
  • 790
  • 1
  • 8
  • 25
  • you declare our variable! #offtop – Medet Tleukabiluly Aug 08 '19 at 06:21
  • I declare variable only in code but who is declare a variable on ram? – Murad Sofiyev Aug 08 '19 at 06:22
  • 1
    That sentence is the summary of what is said before in `1. Encountering var a, Compiler asks Scope to see if a variable a already exists for that particular scope collection. If so, Compiler ignores this declaration and moves on. Otherwise, Compiler asks Scope to declare a new variable called a for that scope collection.` – t.niese Aug 08 '19 at 06:35
  • @t.niese I Know this is summary of what is said before. But this is does not make sense. How compiler can ask Scope? This is not Compiler job this is Engine job? Is there wrong when I say? – Murad Sofiyev Aug 08 '19 at 07:00
  • 1
    This probably also be done later at execution time. But as it is already possible in the compiling step why shouldn't it be done there? `How compiler can ask Scope?` the same way you can figure out the scope while reading the code. The scope is nothing defined at runtime, it is already known before that. – t.niese Aug 08 '19 at 07:10
  • I understand Scope create before runtime. But in there problem is compiler job is to compile code to machine code and can add some performance optimization. But create scope or create variable this is not compiler job this is an engine's job. In creation phase engine create variable and scope not compiler. Do you agree with my idea? @t.niese – Murad Sofiyev Aug 08 '19 at 09:46
  • Looks like in that book, "*Compiler*" is a character in a hypothetical play, not literally "the compiler component" of an implementation. That seems to be done for explanation (and maybe entertainment) purposes. It's true that scope resolution can be done at compilation time, if an implementation chooses to do it that way. I see you've already filed an issue on the project's github issue tracker (https://github.com/getify/You-Dont-Know-JS/issues/1497); that's probably the better venue to clarify this. – jmrk Aug 08 '19 at 10:52
  • Thanks I create issue and tweet to getify. I hope he clearify this topic. – Murad Sofiyev Aug 08 '19 at 11:02
  • But code optimization is exactly the point where the compiler needs have in-depth knowledge about the code and its scoping. The only requirement to the generated byte-/machinecode is that it behaves according to how it is described by the programming language, nothing more. So the compiler can remove the variables, change their scoping, ... as long as it does not change the behavior. And the resulting byte-/machinecode does not even need to have the concept of variables and scopes, it could be solved completely different on that level (registers and memory). – t.niese Aug 08 '19 at 11:04
  • Yeah, actually true scope can be done compilation time (we talk about lexical scope not dynamic) also JavaScript work with lexical scope. But this is dependence compiler and environment. I don't think JavaScript do like this ))) – Murad Sofiyev Aug 08 '19 at 11:06
  • JavaScript is statically scoped (lexical scope) so all scoping information is known at compile-time. In dynamically scoped languages the scoping might be known at compile-time, if it cant be determined at compile time then that language will be most likely be only be an interpreted one. But as the question is about JavaScript how is dynamically scoped relevant? JavaScript ist statically scoped so why shouldn't the compiler use that information at compile-time to do optimizations? – t.niese Aug 08 '19 at 11:17
  • @t.niese Yeah I know compiler can change the scope and remove some variables its dependence on a compiler, language and engine. But in there I talk about JavaScript and I think JS compiler(JIT) not define any variable. In there problem getify says own book JavaScript define variables with compiler and after I read him blog there was a conflict in my brain. I hope I'm true and the compiler does not work with defining variables. – Murad Sofiyev Aug 08 '19 at 11:18
  • " JavaScript ist statically scoped so why shouldn't the compiler use that information at compile-time to do optimization?" I think this question-answer about time. Because JS compiler has little time to compile this code. – Murad Sofiyev Aug 08 '19 at 11:25
  • So thanks guys "getify " explain this on twitter. https://twitter.com/getify/status/1159422653628911617 Thanks for help! – Murad Sofiyev Aug 08 '19 at 12:17

0 Answers0