?? 岸邊的風(fēng):個(gè)人主頁
??? 個(gè)人專欄?:《 VUE 》?《 javaScript 》
?? 生活的理想,就是為了理想的生活 !
?? 前言
眾所周知,JavaScript?是一種非常流行??的編程語言,它已經(jīng)成為了網(wǎng)頁開發(fā)的必備技能。但是,在我們從事JavaScript編程的時(shí)候,我們卻沒有完全發(fā)掘和利用它的全部潛力。在本文中,我們將分享一些高級(jí)的JavaScript技巧,希望幫助CSDN的小伙伴更好地理解和掌握JavaScript編程。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-622962.html
?? 關(guān)于JS高級(jí)用法
在學(xué)習(xí)JavaScript的過程中,我們必須了解一些基礎(chǔ)知識(shí),如變量、函數(shù)、類、循環(huán)等。這些基礎(chǔ)知識(shí)是我們使用JavaScript的基礎(chǔ)。但是,在日常的業(yè)務(wù)開發(fā)中,我們需要一些更高級(jí)的技巧來更好地解決問題。
通過閱讀本文,你將了解到JS的高級(jí)知識(shí)點(diǎn)以及實(shí)際應(yīng)用技巧,如高級(jí)數(shù)據(jù)結(jié)構(gòu)和算法、函數(shù)式編程、異步編程和面向?qū)ο缶幊獭N覀儠?huì)利用代碼實(shí)例來讓大家更好地理解這些知識(shí)點(diǎn)。同時(shí),我們也會(huì)提供一些實(shí)戰(zhàn)案例的示范和使用技巧,讓你更好地將這些技術(shù)應(yīng)用到實(shí)際業(yè)務(wù)中。
?? 高級(jí)數(shù)據(jù)結(jié)構(gòu)和算法
?? Map和Set數(shù)據(jù)結(jié)構(gòu)
在JavaScript中,Map數(shù)據(jù)結(jié)構(gòu)通常用于存儲(chǔ)鍵值對(duì),它可以使用任意類型作為鍵和值。Set數(shù)據(jù)結(jié)構(gòu)用于存儲(chǔ)唯一值的集合。
//?創(chuàng)建Map對(duì)象
const?map?=?new?Map();
//?設(shè)置鍵值對(duì)
map.set('name',?'Tom');
map.set('age',?20);
//?獲取鍵值對(duì)
console.log(map.get('name'));?//?'Tom'
console.log(map.get('age'));?//?20
//?創(chuàng)建Set對(duì)象
const?set?=?new?Set();
//?添加元素
set.add(10);
set.add(20);
set.add(30);
//?刪除元素
set.delete(20);
//?判斷元素是否存在
console.log(set.has(10));?//?true
console.log(set.has(20));?//?false
?? 堆、棧和隊(duì)列
堆和棧是常用的內(nèi)存分配方式。棧是一種后進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),堆是一種動(dòng)態(tài)分配的內(nèi)存結(jié)構(gòu)。隊(duì)列是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),它通常用于緩存和并發(fā)編程中。
//?使用數(shù)組模擬堆
const?arr?=?[1,?2,?3,?4];
arr.push(5);?//?入堆
console.log(arr.pop());?//?出堆
//?使用數(shù)組模擬棧
const?stack?=?[1,?2,?3,?4];
stack.push(5);?//?入棧
console.log(stack.pop());?//?出棧
//?使用數(shù)組模擬隊(duì)列
const?queue?=?[1,?2,?3,?4];
queue.push(5);?//?入隊(duì)
console.log(queue.shift());?//?出隊(duì)
?? 深度優(yōu)先搜索和廣度優(yōu)先搜索
深度優(yōu)先搜索(DFS)和廣度優(yōu)先搜索(BFS)是常用的遍歷算法。DFS通常用于解決深度問題,BFS適用于寬度問題。
//?深度優(yōu)先遍歷
function?dfs(node)?{
??if?(node?==?null)?return;
??console.log(node.value);
??dfs(node.left);
??dfs(node.right);
}
//?廣度優(yōu)先遍歷
function?bfs(node)?{
??const?queue?=?[node];
??while?(queue.length)?{
????const?curr?=?queue.shift();
????console.log(curr.value);
????if?(curr.left)?queue.push(curr.left);
????if?(curr.right)?queue.push(curr.right);
??}
}
?? 常用算法
常用的算法有排序、搜索、查找等。
// 排序算法:快速排序使用分治思想,通過把數(shù)組分成較小的塊來排序。
function?quickSort(arr)?{
??if?(arr.length?<?2)?{
????return?arr;
??}
??let?pivot?=?arr[0];
??let?left?=?[];
??let?right?=?[];
??for?(let?i?=?1;?i?<?arr.length;?i++)?{
????if?(arr[i]?<?pivot)?{
??????left.push(arr[i]);
????}?else?{
??????right.push(arr[i]);
????}
??}
??return?[...quickSort(left),?pivot,?...quickSort(right)];
}
//?查找算法:
function?binarySearch(arr,?target)?{
??let?left?=?0;
??let?right?=?arr.length?-?1;
??while?(left?<=?right)?{
????const?mid?=?Math.floor((left?+?right)?/?2);
????if?(arr[mid]?===?target)?{
??????return?mid;
????}?else?if?(arr[mid]?<?target)?{
??????left?=?mid?+?1;
????}?else?{
??????right?=?mid?-?1;
????}
??}
??return?-1;
}
?? 函數(shù)式編程
?? 高階函數(shù)和柯里化
高階函數(shù)和柯里化是函數(shù)式編程中的常見概念,它們可以讓我們創(chuàng)建更加抽象、靈活的函數(shù)。
//?高階函數(shù)
function?higherOrderFunction(func)?{
??return?function?(num)?{
????return?func(num);
??};
}
function?double(num)?{
??return?num?*?2;
}
const?doubleFunc?=?higherOrderFunction(double);
console.log(doubleFunc(10));?//?20
//?柯里化
function?curry(func)?{
??return?function?curried(...args)?{
????if?(args.length?>=?func.length)?{
??????return?func.apply(this,?args);
????}?else?{
??????return?function?(...args2)?{
????????return?curried.apply(this,?[...args,?...args2]);
??????};
????}
??};
}
function?sum(a,?b,?c)?{
??return?a?+?b?+?c;
}
const?curriedSum?=?curry(sum);
console.log(curriedSum(1)(2)(3));?//?6
?? 閉包和作用域
閉包和作用域是JavaScript中比較常見的概念。閉包可以讓我們維護(hù)函數(shù)內(nèi)的狀態(tài),作用域則決定了變量的可見范圍。
//?閉包
function?closure()?{
??let?i?=?0;
??return?function?()?{
????return?++i;
??};
}
const?func?=?closure();
console.log(func());?//?1
console.log(func());?//?2
//?作用域
let?a?=?10;
function?foo()?{
??let?a?=?20;
??console.log(a);?//?20
}
foo();
console.log(a);?//?10
?? 函數(shù)式編程中的常見模式
函數(shù)式編程中有很多常見的模式,如map、filter、reduce等。
//?map
const?arr?=?[1,?2,?3];
const?mapArr?=?arr.map((item)?=>?item?*?2);
console.log(mapArr);?//?[2,?4,?6]
//?filter
const?filterArr?=?arr.filter((item)?=>?item?>?1);
console.log(filterArr);?//?[2,?3]
//?reduce
const?reduceArr?=?arr.reduce((sum,?curr)?=>?sum?+?curr,?0);
console.log(reduceArr);?//?6
?? 異步編程
?? Promise 和 async / await
Promise和async/await是常見的異步編程方式,它們可以讓我們更好地處理異步編程中的問題。
//?Promise
function?promise()?{
??return?new?Promise((resolve,?reject)?=>?{
????setTimeout(()?=>?{
??????resolve('done');
????},?1000);
??});
}
promise().then((result)?=>?console.log(result));?//?'done'
//?async/await
async?function?asyncFunc()?{
??const?result?=?await?promise();
??console.log(result);
}
asyncFunc();?//?'done'
?? 事件循環(huán)和EventEmitter
事件循環(huán)和EventEmitter用于處理異步事件,它們可以讓我們更好地處理事件流。
//?事件循環(huán)
console.log('start');
setTimeout(()?=>?{
??console.log('setTimeout');
},?0);
Promise.resolve().then(()?=>?console.log('promise'));
console.log('end');
//?EventEmitter
const?{?EventEmitter?}?=?require('events');
const?emitter?=?new?EventEmitter();
emitter.on('doSomething',?(arg1,?arg2)?=>?{
??console.log(`${arg1}?${arg2}`);
});
emitter.emit('doSomething',?'Hello',?'World');?//?'Hello?World'
?? Web Worker
Web Worker可以讓我們將長時(shí)間運(yùn)行的任務(wù)移出主線程,以避免阻塞UI。
//?主線程
const?worker?=?new?Worker('worker.js');
worker.onmessage?=?(event)?=>?{
??console.log(event.data);
};
worker.postMessage('start');
//?worker.js
self.onmessage?=?(event)?=>?{
??const?result?=?longCalculation(event.data);
??self.postMessage(result);
};
?? 面向?qū)ο缶幊?/h2>
?? 類和繼承
JavaScript中的類和繼承與其他面向?qū)ο缶幊陶Z言類似。
//?類
class?Animal?{
??constructor(name)?{
????this.name?=?name;
??}
??speak()?{
????console.log(`${this.name}?makes?a?noise.`);
??}
}
class?Cat?extends?Animal?{
??constructor(name,?breed)?{
????super(name);
????this.breed?=?breed;
??}
??speak()?{
????console.log(`${this.name}?meows.`);
??}
??get?description()?{
????return?`${this.name}?is?a?${this.breed}?cat.`;
??}
??set?nickname(nick)?{
????this.name?=?nick;
??}
}
const?cat?=?new?Cat('Fluffy',?'Persian');
cat.speak();?//?'Fluffy?meows.'
console.log(cat.description);?//?'Fluffy?is?a?Persian?cat.'
cat.nickname?=?'Fuffy';
console.log(cat.name);?//?'Fuffy'
?? Encapsulation、Inheritance、Polymorphism(封裝、繼承、多態(tài))
封裝、繼承、多態(tài)是面向?qū)ο缶幊讨械闹匾拍睢?/p>
//?封裝
class?Person?{
??constructor(name)?{
????this._name?=?name;
??}
??get?name()?{
????return?this._name.toUpperCase();
??}
??set?name(newName)?{
????this._name?=?newName;
??}
}
const?person?=?new?Person('John');
console.log(person.name);?//?'JOHN'
person.name?=?'Lisa';
console.log(person.name);?//?'LISA'
//?繼承
class?Shape?{
??constructor(color)?{
????this.color?=?color;
??}
??draw()?{
????console.log('Drawing?a?shape...');
??}
}
class?Circle?extends?Shape?{
??constructor(color,?radius)?{
????super(color);
????this.radius?=?radius;
??}
??draw()?{
????console.log(`Drawing?a?${this.color}?circle?with?radius?${this.radius}.`);
??}
}
const?circle?=?new?Circle('red',?10);
circle.draw();?//?'Drawing?a?red?circle?with?radius?10.'
//?多態(tài)
function?drawShape(shape)?{
??shape.draw();
}
drawShape(new?Shape('blue'));?//?'Drawing?a?shape...'
drawShape(new?Circle('green',?20));?//?'Drawing?a?green?circle?with?radius?20.'
???總結(jié)和實(shí)戰(zhàn)
在本文中,我們介紹了一些JavaScript的高級(jí)知識(shí)點(diǎn),如高級(jí)數(shù)據(jù)結(jié)構(gòu)和算法、函數(shù)式編程、異步編程和面向?qū)ο缶幊?。我們還提供了一些代碼示例和實(shí)戰(zhàn)案例,讓掘友們更好地理解和掌握這些技術(shù)。
?? 通過Promise.all實(shí)現(xiàn)并發(fā)請(qǐng)求
function?fetchData(urls)?{
??const?promises?=?urls.map((url)?=>?fetch(url));
??return?Promise.all(promises).then((responses)?=>
????Promise.all(
??????responses.map((response)?=>?{
????????if?(!response.ok)?throw?new?Error(response.statusText);
????????return?response.json();
??????})
????)
??);
}
?? 使用?async / await?實(shí)現(xiàn)異步調(diào)用
async?function?getData(url)?{
??const?response?=?await?fetch(url);
??if?(!response.ok)?throw?new?Error(response.statusText);
??const?data?=?await?response.json();
??return?data;
}
?? 在面向?qū)ο缶幊讨惺褂?span style="color:#fe2c24;">工廠模式
class?Product?{
??constructor(name,?price)?{
????this.name?=?name;
????this.price?=?price;
??}
}
class?ProductFactory?{
??createProduct(name,?price)?{
????return?new?Product(name,?price);
??}
}
const?productFactory?=?new?ProductFactory();
const?product?=?productFactory.createProduct('Apple',?1);
console.log(product.name);?//?'Apple'
console.log(product.price);?//?1
以上是一些簡(jiǎn)單的實(shí)戰(zhàn)示例,但實(shí)際開發(fā)中,我們需要更加復(fù)雜和具體的案例來應(yīng)對(duì)實(shí)際問題。希望本文能夠?yàn)樽x者提供一些參考,讓大家更好地掌握J(rèn)avaScript的高級(jí)用法,像大神一樣使用JavaScript進(jìn)行開發(fā)。
???寫在最后
在掌握一些高級(jí)技巧之后,還應(yīng)該注重代碼質(zhì)量與可維護(hù)性等方面。我們可以采用一些工具和規(guī)范來幫助我們改善代碼質(zhì)量,例如ESLint、Prettier、代碼規(guī)范等。只有在代碼質(zhì)量和可維護(hù)性上下功夫,我們才能成為真正的JavaScript大神。
?文章來源:http://www.zghlxwxcb.cn/news/detail-622962.html
?
到了這里,關(guān)于發(fā)掘JavaScript潛力:掌握高級(jí)技巧,成為JavaScript編程大師!的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!