"You do not know JavaScript" notes (a)

With a week to "you do not know JavaScript," read, but left a lot of doubts, so he took the puzzled look back at the contents of JavaScript, gained slightly.

The second time reading this book, I hope to be able to have a more profound understanding.

Lexical scoping

...... If there is a state of the resolution process, but also gives the semantics of the word ......

Part of the contents of this book is compiled in conjunction with the principle of JavaScript to the opening, so if not studied compiler theory, this small part of the contents of a bit obscure.

Although most people do not come into contact with compiler theory, but there is one thing must know, markdown syntax. In fact, from markdown to HTML file contains the process of lexical process.

What is the status? Roughly speaking, it is the problem of matching a pattern, or may be considered to match the string:

The source string: "str782yui"string to be matched:"sj1" , think about simple matching algorithm, we have to start from scratch to compare, then every time the comparison, have two emotions, characters with the same / different, and each accept a character, you It will go to one of the states.

Simple pattern matching

This is called state. Of course, this is only a superficial analogy, there may be better.

To tell the truth, compiler theory is useful subjects, but time really learn it was quite painful.

Consider the following code:

function foo(a) {
  var b = a*2;
  function bar(c) {
      console.log(a, b, c);
  }
  bar(b*3);
}
foo(2);

There are three stepwise in this example, the nested scope ......

For java programmers, a pair of curly braces you can limit the scope of the variable, and the relationship between the scopes have sibling and parent-child are two, as well as package (package) This is very convenient thing.

However, JavaScript is not the same, a pair of curly braces is not a defined scope, and enhance the presence of variable var variable declarations, so sometimes we will find a variable that can not be confined in a flower as expected brackets, so it was a lot of classic question.

The JavaScript function block and catch the child is able to create a scope, but personally believe that, in order to create a scope and use of catch sub-block, which is tantamount to catch adds a semantic ambiguity can not say, which may cause the program become a bad read. But we all do, then I will go with the flow of it.

Think one does not know where to pass out of the joke, she said: catch exception handling is not the key, but the flow control statements.

Referred to the scope, be sure to mention the variable shield , it simply on the word: internal scope variables of the same name will be shielded external variable scope .

That someone might ask, (above code) I pass foo.bthis way can not access it?

...... I want to Shane? I think the object is not it? But you can window.fooaccess the global variables in this way foo, why? This windowis not a global object Well, all global variables will automatically become the property of the global object.

...... lexical scoping find only find an identifier ......

This sentence is the answer to the question I raised above.

function foo(str, a) {
  eval(str);
  console.log(a, b);
}
var b = 2;
foo("var b = 3", 1);

eval(..)In the call " var b = 3;" This diamante will be treated the same as already where ......

The statement is a good mouthful ah?

Sometimes written procedures, I would think, gee, if a string can become a variable like, how convenient ah.

eval is almost completed it. The book says so around, presumably to allow the reader a better understanding.

In my opinion, eval function does is dynamically generates a piece of code inserted into the corresponding position, into another program, do the same execution.

with the statement it is actually based on objects you pass it out of thin air to create a new lexical scope .

Here added a keyword to create scope.

var obj = {
    a: 1
};
with(obj) {
    a = 2;
    b = 3;
};

The above code, we can understand, with the obj declared as a scope inside the with statement are in the scope of, so ......

eval (..) and with lexical scope defined when modify or create a new scope at runtime, in order to deceive other writing.

eval is modified, with a scope is created.

Both programs will lead to a decline in performance due to the impact of compiler optimization , in fact, maybe like a switch, when there is any one, will prohibit open compiler optimization button. Like the so-called ban command rearrangement of the same.

to sum up

| ू · ω · `): This book is really nice.

Guess you like

Origin www.cnblogs.com/keepsmart/p/11615964.html