自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

18個JavaScript技巧:編寫簡潔高效的代碼

開發(fā) 前端
在這篇文章中,我將分享18個JavaScript技巧,以及一些你應(yīng)該知道的示例代碼,以編寫簡潔高效的代碼。

本文翻譯自 18 JavaScript Tips : You Should Know for Clean and Efficient Code,作者:Shefali, 略有刪改。

在這篇文章中,我將分享18個JavaScript技巧,以及一些你應(yīng)該知道的示例代碼,以編寫簡潔高效的代碼。

讓我們開始吧!??

箭頭函數(shù)

可以使用箭頭函數(shù)來簡化函數(shù)聲明。

function add(a, b) {
  return a + b;
}

// Arrow function
const add = (a, b) => a + b;

Array.from()

Array.from()方法可用于將任何可迭代對象轉(zhuǎn)換為數(shù)組。

const str = "Hello!";
const arr = Array.from(str);

console.log(arr); //Output: ['H', 'e', 'l', 'l', 'o', '!']

使用console.table顯示數(shù)據(jù)

如果您希望在控制臺中組織數(shù)據(jù)或以表格格式顯示數(shù)據(jù),則可以使用console.table()。

const person = {
    name: 'John', 
    age: 25,
    profession: 'Programmer'
}
console.table(person);

輸出效果:

圖片圖片

使用const和let

對于不會被重新分配的變量使用const

const PI = 3.14;
let timer = 0;

使用解構(gòu)提取對象屬性

通過使用解構(gòu)從對象中提取屬性,可以增強代碼的可讀性。

const person = {
    name: 'John', 
    age: 25,
    profession: 'Programmer'
}

//Instead of this ??
console.log(person.name);
console.log(person.age);

//Use this??
const {name, age} = person;
console.log(name);
console.log(age);

使用邏輯OR運算符設(shè)置默認值

使用||操作符輕松設(shè)置默認值。

function greet(name) {
  name = name || 'Person';
  console.log(`Hello, ${name}!`);
}

greet(); //Output: Hello, Person!
greet("John"); //Output: Hello, John!

清空數(shù)組

你可以使用length屬性輕松清空數(shù)組。

let numbers = [1, 2, 3, 4];
numbers.length = 0;
console.log(numbers); //Output: []

JSON.parse()

使用JSON.parse()將JSON字符串轉(zhuǎn)換為JavaScript對象,這確保了無縫的數(shù)據(jù)操作。

const jsonStr = '{"name": "John", "age": 25}';
const person = JSON.parse(jsonStr);
console.log(person); 
//Output: {name: 'John', age: 25}

Map()函數(shù)

使用map()函數(shù)轉(zhuǎn)換新數(shù)組中的元素,而不修改原始數(shù)組。

const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(numbers); //Output: [1, 2, 3, 4]
console.log(doubled); //Output: [2, 4, 6, 8]

Object.seal()

您可以使用Object.seal()方法來防止在對象中添加或刪除屬性。

const person = {
    name: 'John', 
    age: 25
};
Object.seal(person);
person.profession = "Programmer";
console.log(person); //Output: {name: 'John', age: 25}

Object.freeze()

您可以使用Object.freeze()方法來阻止對對象的任何更改,包括添加,修改或刪除屬性。

const person = {
    name: 'John', 
    age: 25
};
Object.freeze(person);
person.name = "Mark";
console.log(person); //Output: {name: 'John', age: 25}

刪除數(shù)組重復(fù)項

您可以使用Set從數(shù)組中刪除重復(fù)的元素。

const arrWithDuplicates = [1, 12, 2, 13, 4, 4, 13];
const arrWithoutDuplicates = [...new Set(arrWithDuplicates)];
console.log(arrWithoutDuplicates); 
//Output: [1, 12, 2, 13, 4]

使用解構(gòu)交換值

你可以使用解構(gòu)輕松地交換兩個變量。

let x = 7, y = 13;
[x, y] = [y, x];
console.log(x); //13

擴展運算符

您可以使用擴展運算符有效地復(fù)制或合并數(shù)組。

const arr1 = [1, 2, 3];
const arr2 = [9, 8, 7];

const arr3 = [...arr2];
const mergedArr = [...arr1, ...arr2];

console.log(arr3); //[9, 8, 7]
console.log(mergedArr); //[1, 2, 3, 9, 8, 7]

模板字符串

利用模板文字進行字符串插值并增強代碼可讀性。

const name = 'John';
const message = `Hello, ${name}!`;

三元運算符

可以用三元運算符簡化條件語句。

const age = 20;

//Instead of this??
if(age>=18){
    console.log("You can drive");
}else{
    console.log("You cannot drive");
}

//Use this??
age >= 18 ? console.log("You can drive") : console.log("You cannot drive");

使用===代替==

通過使用嚴格相等(===)而不是==來防止類型強制轉(zhuǎn)換問題。

const num1 = 5;
const num2 = '5';

//Instead of using ==
if (num1 == num2) {
  console.log('True');
} else {
  console.log('False');
}

//Use ===
if (num1 === num2) {
  console.log('True');
} else {
  console.log('False');
}

使用語義化變量和函數(shù)名稱

為變量和函數(shù)使用有意義的描述性名稱,以增強代碼的可讀性和可維護性。

// Don't declare variable like this
const a = 18;

// use descriptive names
const numberOfTips = 18;

今天的內(nèi)容就到這里,希望對你有幫助。

責任編輯:武曉燕 來源: 南城大前端
相關(guān)推薦

2022-08-28 19:03:18

JavaScript編程語言開發(fā)

2019-07-31 10:24:16

JavaScript瀏覽器口袋妖怪

2020-08-06 16:34:48

Python開發(fā)工具

2022-05-10 10:28:21

JavaScript代碼

2024-03-28 14:29:46

JavaScript編程

2023-08-27 16:19:09

JavaScript編程語言

2024-12-04 15:10:21

2021-02-23 10:48:30

Python代碼開發(fā)

2020-12-07 08:01:59

JavaScript入門技巧

2014-11-10 09:59:08

jQuery

2023-07-30 17:10:32

TypeScript開發(fā)

2019-03-19 13:44:41

Python編程技巧編程語言

2022-02-24 10:05:20

Python編程語言代碼

2021-04-25 11:31:45

React代碼整潔代碼的實踐

2023-06-19 15:36:30

JavaScrip技巧開發(fā)

2022-06-27 06:23:23

代碼編程

2022-12-15 10:52:26

代碼開發(fā)

2014-07-29 13:55:10

程序員代碼

2024-10-15 10:51:47

2021-12-27 14:33:47

Python語言開發(fā)
點贊
收藏

51CTO技術(shù)棧公眾號