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中比较常见的概念。闭包可以让我们维护函数内的状态,作用域则决定了变量的可见范围。
// 闭包
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
#### 📟 函数式编程中的常见模式
函数式编程中有很多常见的模式,如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’
#### 📟 事件循环和EventEmitter
事件循环和EventEmitter用于处理异步事件,它们可以让我们更好地处理事件流。
// 事件循环
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可以让我们将长时间运行的任务移出主线程,以避免阻塞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);
};
## 📘 面向对象编程
#### 📟 类和继承
JavaScript中的类和继承与其他面向对象编程语言类似。
// 类
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(封装、继承、多态)
封装、继承、多态是面向对象编程中的重要概念。
// 封装
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.’
// 多态
function drawShape(shape) {
shape.draw();
}
drawShape(new Shape(‘blue’)); // ‘Drawing a shape…’
drawShape(new Circle(‘green’, 20)); // ‘Drawing a green circle with radius 20.’
## **🔥**总结和实战
>
> 在本文中,我们介绍了一些JavaScript的高级知识点,如高级数据结构和算法、函数式编程、异步编程和面向对象编程。我们还提供了一些代码示例和实战案例,让掘友们更好地理解和掌握这些技术。
>
>
>
#### 📖 通过Promise.all实现并发请求
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 实现异步调用
async function getData(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(response.statusText);
const data = await response.json();
return data;
}
#### 📖 在面向对象编程中使用工厂模式
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
>
> **以上是一些简单的实战示例,但实际开发中,我们需要更加复杂和具体的案例来应对实际问题。希望本文能够为读者提供一些参考,让大家更好地掌握JavaScript的高级用法,像大神一样使用JavaScript进行开发。**
>
>
>
## 📚 [写在最后](#%F0%9F%93%98%20%E5%86%99%E5%9C%A8%E6%9C%80%E5%90%8E)
**前端面试题汇总**
![](https://img-blog.csdnimg.cn/img_convert/8087f8c06b129975b3b7be228aa41f0d.png)
**JavaScript**
![](https://img-blog.csdnimg.cn/img_convert/7796de226b373d068d8f5bef31e668ce.png)
**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**
**性能**
![](https://img-blog.csdnimg.cn/img_convert/d7f6750332c78eb27cc606540cdce3b4.png)
**linux**
![](https://img-blog.csdnimg.cn/img_convert/ed368cc25284edda453a4c6cb49916ef.png)
**前端资料汇总**
![](https://img-blog.csdnimg.cn/img_convert/6e0ba223f65e063db5b1b4b6aa26129a.png)