Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 18 good habits of writing JavaScript

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the 18 good habits of writing JavaScript? aiming at this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

In my many years of programming career, I will give priority to the way I write code, not only to make the code more concise, but also because it helps to improve coding efficiency and make code faster.

Writing 1000 lines of code is not the same as writing 300 lines of code, which is very helpful to the program or script we are working on. You must be very careful when programming, because dirty code can be a serious problem in large projects.

Many programmers criticize the JS language for not having the standard way of working like Java or C++, but the truth is that JavaScript is one of the best languages in use today, some of them Facebook and Netflix. Libraries like React improve front-end performance, and as for back-end, nextJs is used to increase speed, a combination that drives today's programmers crazy.

* * ECMAScript V6 (abbreviated as ES6 or ES2015) * * is the standard that JavaScript has been following since June 2015.

By learning the following techniques, we can improve the way we write code. Spend some time practicing on the console and study well.

Let's get started.

1. Constant uses const instead of var

Constants are immutable variables, so declaring variables ensures that they never change.

/ * Old method * / var I = 1; / * correct method * / const I = 1

two。 Replace variables with let instead of var

The let statement declares a local scope variable with block scope

/ * inappropriate * / var myVal = 1; for (var i; i

< 10; i++){ myVal = 1 + i; } /* 正确方式*/ let myVal = 1; for (let i; i < 10; i++){ myVal += i } 3.声明对象 用快捷方式声明对象 /* 旧方法 The Object() class makes an unnecessary function call */ const myObject = new Object(); /* 正确方式*/ const myObject = {}; 5.连接字符串 /* 旧方法 */ const myStringToAdd = "world"; const myString = "hello " + myStringToAdd; /* 正确方式*/ const myStringToAdd = "world"; const myString = `hello ${myStringToAdd}`; 6. 使用对象方法简写 /* 不适当 */ const customObject = { val: 1, addVal: function () { return customObject.val + 1; } } /* 正确方式*/ const customObject = { val: 1, addVal(){ return customObject.val++ } }**加粗文字** 7.创建对象的值 /* 旧方法*/ const value = 1; const myObject = { value: value } /* 正确方式*/ const value = 1; const myObject = { value } 8. 给对象赋值 /* 旧方法 */ const object1 = { val: 1, b: 2 }; let object2 = { d: 3, z: 4 }; object2.val = object1.val; object2.b = object1.b; /* 正确方式 */ const object1 = { val: 1, b: 2 }; const object2 = { ...object1, d: 3, z: 4 } 9. 给数组赋值 /* 不适当*/ const myArray = []; myArray[myArray.length] = "hello world"; /* 正确方式 */ const myArray = []; myArray.push('Hello world'); 10. 连接数组 /* 不适当*/ const array1 = [1,2,3,4]; const array2 = [5,6,7,8]; const array3 = array1.concat(array2); /* 正确方式 */ const array1 = [1,2,3,4]; const array2 = [5,6,7,8]; const array3 = [...array1, ...array2] 11.获取对象的多个属性 /* 不适当*/ function getFullName(client){ return `${client.name} ${client.last_name}`; } /* 正确方式 */ function getFullName({name, last_name}){ return `${name} ${last_name}`; } 12.从对象获取值 /* 不适当*/ const object1 = { a: 1 , b: 2 }; const a = object1.a; const b = object1.b; /* 正确方式 */ const object1 = { a: 1 , b: 2 }; const { a, b } = object1; 13. 创建函数 /* 老办法,但很好 */ function myFunc() {} /* 很好*/ const myFunc = function() {} /* 最好 */ const myFunct = () =>

{} / important note: in some cases, it is recommended that you do not use these functions with arrows to avoid read errors

14. Default value

/ * inappropriate * / const myFunct = (a, b) = > {if (! a) a = 1; if (! b) b = 1; return {a, b};} / * correct way * / const myFunct = (a = 1, b = 1) = > {return {a, b};}

15. Use reduce instead of forEach and for to sum

/ * inappropriate * / const values = [1,2,3,4,5]; let total = 0; values.forEach ((n) = > {total + = n}) / * inappropriate * / const values = [1,2,3,4,5]; let total = 0; for (let i; i)

< values.length; i++){ total += values[i]; } /* 正确方式 */ const values = [1, 2, 3, 4, 5]; const total = values.reduce((total, num) =>

Total + num)

16. Whether it exists in the array

/ * inappropriate * / const myArray = [{a: 1}, {a: 2}, {a: 3}]; let exist = false; myArray.forEach (item = > {if (item.a = 2) exist = true}) / * correct way * / const myArray = [{a: 1}, {a: 2}, {a: 3}]; const exist = myArray.some (item = > item.a = 2)

17. Shortcuts to Boolean values

/ * inappropriate * / const a = 5; let b; if (a = 5) {b = 3;} else {b = 2;} / * correct * / const a = 5; const b = a = = 5? 3: 2 This is the answer to the 18 questions about the good habits of writing JavaScript. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report