Javascript It's powerful and flexible , It allows you to write code in any way you like , And try something very unusual , This can lead to errors in the code . Here's what we learned about Javascript Best practices for writing elegant code .

Always used :use strict

Is likely to , If the project Javascript Use any library / Framework or compiler ,use strict It's not allowed , But just in case , Remember to add it to files and functions . If it's not included , It can go wrong in some scenarios .

Use function expressions instead of function declarations

Unless you want to take advantage of the behavior and properties of a function , Otherwise, it's better to use function expressions . The function declaration is suspended , Even though it can be useful at times , But avoid them , Because they bring strange behavior to the code , And what happens is not always obvious . Try to figure out where the functions used come from , They appear before you use them , To avoid strange visits .

Stop using var

with " var " The statement of the will also be suspended , This makes it possible to var The statement is accessible , It's strange , Non obvious behavior .

Use as much as possible const And invariance

Like invariance as much as possible . Constantly changing data and passing it on makes tracking bug And change itself becomes difficult . Processing data copies , Avoid side effects .

Write more pure functions

Pure function is such a function , The same input , Always get the same output , And there are no side effects to observe .

The side effect is in the calculation process , A change in the state of a system , Or observable interaction with the outside world .

Prefer classes over constructors

Although constructors allow some very good functionality , But if you find yourself using its prototype , That means you need to use what's supported almost anywhere class . It's Cleaner , It's easier to understand .

Use “ deconstruction ”

Deconstruction is elegant , It makes what you get from arrays and objects more obvious , And give you the chance to rename , To make the code more elegant .

Only process the data you need

Like the example above , Deconstruction is a good way to extract data for work , But also get into the habit of calling only what you need for methods and functions . It's also about coming from API The data of . Before storing or doing anything to it , Just extract and clean up the data you need .

Always used :“===”

Triple equality check values and types. This is what you always want to do . Get in the habit of double checking , Avoid adverse effects .

Avoid global variables

Avoid creating things in global objects , Unless you're creating a library / frame . Global property names may conflict with what a third party or colleague introduces , It is difficult to debug .

Organization statement

Keep the way you declare things . Put all statements on top , Start with constants , All the way to variables . Capitalize all constants , To show that they are constants , So developers don't try to change them .

Do not use undefined To initialize the

For variables without values, use undefined . Let's agree that “ No value ” As sth “ value ” It's a very strange concept , Right ? since Javascript Has made things undefined 了 , How do you know if it's you or Javascript Let things go undefined What about it ? This makes debugging why things are undefined It is difficult to , So I'd rather set things as null Instead of .

Always initialize declaration

For the same reason , You should not provide undefined As value , Nor should any values be reserved for them , Because by default they are undefined .


Javascript The flexibility of the Internet has opened the way for me to the front end , Increasingly strict syntax and build tools , Let me keep that passion to explore .