javascript - Questions about decorators in "Introduction to ES6 Standards"
仅有的幸福
仅有的幸福 2017-06-30 09:58:40
0
2
825

The changes made by the decorator to the behavior of the class occur when the code is compiled, not at runtime. This means that decorators can run code during compilation. In other words, the decorator is essentially a function that is executed at compile time.

There are several questions:

  1. It is said in the above quote: The change of the behavior of the class by the decorator occurs when the code is compiled, not at runtime, the compile time# in this sentence ##, Runtime, occurrenceWhat do they mean?

  2. The essence of a decorator is a function that is executed during compilation. Does the execution here mean the same thing as the occurrence in question 1?

  3. Where are modifiers generally used?

The explanation in the book is rather vague, please explain it in detail, thank you very much!

仅有的幸福
仅有的幸福

reply all(2)
我想大声告诉你

Compilation time can be roughly understood as when the JS engine converts the code into operations that the machine can run before the code starts running. In other words, this process cannot be interfered with at runtime because it is completed before it starts running.

Runtime refers to when the operations that the machine can run start to run after your code is compiled, that is, when the things you write change from machine operations to things that you can actually see on the computer.

As for occurrence, there is no special meaning, it means that these changes are completed when the JS engine converts the code into machine operations.

Execution and occurrence can be roughly considered to be the relationship between cause and effect. Change, the general collocation of the verb is "happen", function, the general collocation of the word is "execution". Occurrence emphasizes the contrast between the before and after state, that is, because something happened, the afterwards is different from the before. Execution, on the other hand, focuses on the process. To perform an operation is to do every step in the operation again, without deliberately paying attention to whether there are any differences before and after. Therefore, here should be the decorator = the function executed at compile time. The result of executing this function is that the class behavior changes.

Since, as mentioned above, it is a change to class behavior, so modifiers are generally used to modify methods and attributes in a class when it is declared.

为情所困

To put it bluntly, the code is generally divided into a compilation stage and a running stage. You can understand that the compilation stage parses the code into a tree, and when it comes to the running stage, it is run according to the tree formed by compilation. You can search for core-decorators.js on github. This library is an example of a decorator.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template