18個JavaScript編寫簡潔高效代碼的技巧分享
本文翻譯自 18 JavaScript Tips : You Should Know for Clean and Efficient Code,作者:Shefali, 略有刪改。
箭頭函數(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)從對象中提取屬性,可以增強(qiáng)代碼的可讀性。
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è)置默認(rèn)值
使用||
操作符輕松設(shè)置默認(rèn)值。
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
擴(kuò)展運算符
您可以使用擴(kuò)展運算符有效地復(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]
模板字符串
利用模板文字進(jìn)行字符串插值并增強(qiáng)代碼可讀性。
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");
使用===代替==
通過使用嚴(yán)格相等(===)而不是==
來防止類型強(qiáng)制轉(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ù)使用有意義的描述性名稱,以增強(qiáng)代碼的可讀性和可維護(hù)性。
// Don't declare variable like this const a = 18; // use descriptive names const numberOfTips = 18;
以上就是18個JavaScript編寫簡潔高效代碼的技巧分享的詳細(xì)內(nèi)容,更多關(guān)于JavaScript技巧的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript+Canvas創(chuàng)建一個獨特的字符畫生成器
這篇文章主要介紹了如何使用 Canvas 和 JavaScript 創(chuàng)建一個獨特的字符畫生成器,通過此生成器,我們可以將圖片轉(zhuǎn)換為由字符構(gòu)成的作品,感興趣的可以了解下2024-01-01KnockoutJS 3.X API 第四章之表單textInput、hasFocus、checked綁定
這篇文章主要介紹了KnockoutJS 3.X API 第四章之表單textInput、hasFocus、checked綁定的相關(guān)資料,需要的朋友可以參考下2016-10-10JavaScript統(tǒng)計字符出現(xiàn)次數(shù)
這篇文章主要為大家詳細(xì)介紹了JavaScript字符統(tǒng)計出現(xiàn)次數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03JavaScript中的this/call/apply/bind的使用及區(qū)別
這篇文章主要介紹了JavaScript中的this/call/apply/bind的使用及區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03js ondocumentready onmouseover onclick onmouseout 樣式
下面都是一些上面的事件觸發(fā)的事先定義的代碼。2010-07-07Bootstrap table 服務(wù)器端分頁功能實現(xiàn)方法示例
這篇文章主要介紹了Bootstrap table 服務(wù)器端分頁功能實現(xiàn)方法,結(jié)合實例形式詳細(xì)分析了Bootstrap table 服務(wù)器端后臺交互與分頁功能相關(guān)操作技巧,需要的朋友可以參考下2020-06-06JavaScript中使用Object.prototype.toString判斷是否為數(shù)組
這篇文章主要介紹了JavaScript中使用Object.prototype.toString判斷是否是數(shù)組,本文講解了Object.prototype.toString相關(guān)知識,并給出了判斷數(shù)組的實現(xiàn)代碼,使用本文方法同樣可以判斷javascrpty的其它數(shù)據(jù)類型,需要的朋友可以參考下2015-04-04