In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, Xiaobian will bring you about how to avoid writing bad code in JavaScript. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
Alternative classes with different interfaces
Because of duplication, two classes with the same functionality but different interfaces are not good.
We don't want that. Therefore, we might want to create a superclass using shared code, and then make the subclass have different methods.
Library class incomplete
Reuse is not overrated.
Library builders have a hard job. They may have incomplete courses, but they don't allow us to modify them to accomplish what we want them to do.
Therefore, this makes library classes useless to us unless we can add the required functionality.
We may have to add new methods directly to these classes to solve this problem.
For example, if we import a class, we can add our own methods by writing:
const mixin = { foo() { //... }, bar() { //... } } Object.assign(Foo.prototype, mixin);
In the code above, we merged code from the prototype of the Foo class with methods from the mixin object to incorporate more methods into the class using the Object.assign method.
data type
A data class is a class that has only fields.
These classes may be manipulated excessively by other classes.
Therefore, if all public domains are public, we should encapsulate them.
We can also encapsulate collection fields if desired.
To encapsulate them, we can make fields private and add methods to access and set them.
Denial of inheritance
Subclasses inherit methods that their parent class can access.
If we don't need these classes in the parent class, we can push them down into the child class.
However, not all subclasses will inherit methods from the parent class, and they can still remain in subclasses that need them.
annotation
Comments are useful for certain things. We can comment on why we want to do something, but since we already do it in code, we don't need to explain how we do it in our comments.
Also comment code is bad. First of all, we should take them away because they are not running.
Conditions that do not belong to you
We should break conditional statements into their own lines so that we can read them more easily.
So don't write:
if (foo) { //... } if (bar) { //... }
We write:
if (foo) { //... } if (bar) { //... }
Comments Optional Parameters
Optional parameters should have default values in JavaScript. For example, we could write the following code to indicate that the parameter is optional:
const foo = (a = 1) => { //... }
> Photo by Samantha Gades on Unsplash
Beware of "Dead Storage"
Dead storage means that we assign values to variables but reassign them without using the original values.
Therefore, we don't really need the original value, so we can delete the row.
So don't write:
let x = 1; x = 8 * 10;
We write:
let x = 8 * 10;
Do not invert our Boolean values
Double negatives are always harder to read than direct conditional expressions.
Therefore, it is possible that we should write in a more direct way. For example, instead of writing:
if (! (x > 10)) { //... }
We write:
if (x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.