YK fungus spent more than ten days summarizing Teacher Yuying’s three-hour course, replicating most cases and thinking about them. Today, I come to make a comprehensive summary of my previous blog posts and teacher Yuying’s course on “How to Write JS Well”.
BB sentence: still remember finish the class in the course opinion of punch card, I wrote: light moon shadow teacher’s class I can write eight blog posts! Unexpectedly, I actually wrote, this is the eighth!!
We’ve covered seven principles or techniques for writing good JavaScript:
- Component encapsulation 3. Process Abstraction 4. Style First 5. Make it right 6. Encapsulate functions 7
The company accountable
First of all, we should do their own responsibilities. If we encounter pure UI display operations, page style switching, animation and so on, we can give priority to using CSS to operate, rather than JavaScript! The best way to solve a bug is not to write code
Ensure that the correct
The most important thing is to ensure that the code we write is correct, and not to write an incorrect code. In some complicated cases, considering the mathematical model of the algorithm, it is best to give proof to ensure the mathematical correctness of the code.
Style is preferred
In actual team project development, it is necessary to set up code specification verification to unify the style and increase code readability for better cooperation. That is, style first
We handle some Numbers on the operation by considering a little bit more at the bottom, and how we can improve the code efficiency, using an operation is the choice of a good promote efficiency, but we can not blindly pursue perfection at the expense of the efficiency of the readability of the code, code readability and efficiency is also equally important!
Of course, there is no right answer to the question of readability versus efficiency depending on the usage scenario.
Packaging function
When we define a function, we should do a good job of wrapping the function, try not to let a function directly handle an external variable, it is best to change the variable into a parameter, passed to the function. Data abstraction and procedure abstraction should be done when encapsulating functions.
doData abstractionTo separate out some of the data used, define an object array outside the function and use it in the function to decouple the data and improve the reusability of the function.
Procedural abstraction
Not only can you do data abstraction, you can also doProcedural abstractionAbstract some operations into a single function, that is, decouple the operation behavior from the function, introduce where necessary (only care What to do not care How to do), can also improve code reusability.
In our code base, write as many pure functions with no side effects as possible, non-invasive code. Can make our code more maintainable.
You can write in JavaScriptimperative(How to do) code, can also be writtendeclarative(What to do) code, when dealing with complex logic, the recommended use of declarative, more abstract,expandingstronger
Component packaging
In the process of component encapsulation, HTML decoupling is template-oriented, JavaScript decoupling is plug-in oriented, and common component model is abstracted to form component framework, which increases component reusability and flexibility.
Use features
Finally, JavaScript has many features of its own, such as re, JSON, various methods on arrays, and so on. Using features when we write code can greatly improve our productivity.
Related blog
[Youth Training camp] Teacher Yue Ying told me the three principles of writing good JavaScript — each is responsible for his own work
[Youth Training Camp] Teacher Yue Ying told me the three principles of good JavaScript writing — component encapsulation
[Youth Training Camp] Teacher Yue Ying told me the three principles of good JavaScript – process abstraction
[Youth Training Camp] Teacher Yue Ying told me four skills to write good JavaScript — style first
[Youth Training Camp] Teacher Yue Ying told me four skills to write good JavaScript — make sure it is correct
[Youth Training Camp] Teacher Yue Ying told me the four skills to write good JavaScript – encapsulation function
[Youth Training camp] Teacher Yue Ying told me the four skills to write good JavaScript – useful features
You can also pay attention to the column: [Youth Training Camp notes]