Skip to content

Commit e5832bf

Browse files
committed
chore(ci): blog sync
1 parent 6ce97c2 commit e5832bf

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

data/blog/ES6dixin特xing.mdx

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: ES6的新特性
3+
date: 2023-12-23T12:48:31Z
4+
summary:
5+
tags: []
6+
---
7+
8+
## 数组的解构
9+
```
10+
const arr = ["item1","item2","item3"]
11+
//当值not defined时给它一个默认值
12+
const [item1,item2,item3,item4 = "abc"] = arr
13+
14+
console.log(item1,item2,item3,item4);
15+
//输出item1 item2 item3 abc
16+
17+
console.log(...arr);
18+
//输出item1 item2 item3
19+
```
20+
## 对象的解构
21+
```
22+
const obj = {
23+
name: "in wind",
24+
age: 18,
25+
height: 18.8,
26+
};
27+
const {name,age,height} = obj
28+
29+
console.log(name,age,height);
30+
//输出in wind 18 18.8
31+
//配置别名和默认值
32+
const {sex:mySex = "男"} = obj
33+
34+
console.log(mySex);
35+
//输出 男
36+
```
37+
## const/let
38+
const定义的是一个常量,定义的值是不可以被修改的
39+
那为什么定义一个对象或者数组的时候,可以修改对象的属性或者数组的某个值呢
40+
因为const定义对象或者数组的时候,保存的是他们的内存地址,指向堆中创建出的的数组或对象
41+
进行修改时,该指针并不会改变
42+
let定义一个变量
43+
## var和let/const的区别
44+
1、var能重复定义,而let和const不行
45+
2、var会造成变量提升,即在声明变量之前能被访问,而let和const没有变量提升,虽然他被创建出来了,但不能被访问到
46+
## 块级作用域
47+
function(){} {} if(){} for(){}
48+
## 暂时性死区
49+
```
50+
var a = "abc"
51+
if(true){
52+
//在一个块级作用域内,如果在let和const之前使用声明的变量,就会产生暂时性死区
53+
console.log(a)
54+
let a = "cba"
55+
}
56+
```
57+
## for of 和 for in
58+
```
59+
const arr = [1,2,3,4,5]
60+
const obj = {a:"aaa",b:"bbb",c:"ccc"}
61+
//for of遍历数组
62+
for(const item of arr){
63+
console.log(item);
64+
//输出数组的值1,2,3,4,5
65+
}
66+
//for of遍历对象会报错,因为没有迭代器对象
67+
for(const item in arr){
68+
console.log(item);
69+
//输出数组的key
70+
}
71+
72+
for(const item in obj){
73+
console.log(item)
74+
//输出对象的key
75+
}
76+
```
77+
## 模板字符串
78+
```
79+
//模板字符串
80+
const name = "inwind"
81+
console.log(`这是${name}的博客`)
82+
//输出 这是inwind的博客
83+
84+
//标签模块字符串
85+
function foo(m,n,y){
86+
console.log(m,n,y);
87+
//输出[ '我是', '身高', '米' ] inwind 18.8
88+
}
89+
const name = "inwind"
90+
const height = 18.8
91+
foo`我是${name}身高${height}米
92+
```
93+
## 函数参数的默认值
94+
基本的就不提了,说一些细节
95+
```
96+
函数的length
97+
function foo(a,b,c="1.88"){
98+
99+
}
100+
console.log(foo.length);
101+
//被设置默认值的参数是不会被算的函数length里的
102+
```
103+
## 函数的剩余参数
104+
```
105+
function foo(a,b,...args){
106+
console.log(args)
107+
//输出['c','d','e','f','g']
108+
}
109+
110+
foo("a","b","c","d","e","f","g")
111+
```
112+

data/blog/hanshudikelihua.mdx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 函数的柯里化
3+
date: 2023-12-23T12:48:50Z
4+
summary:
5+
tags: []
6+
---
7+
8+
## 柯里化?
9+
函数的柯里化指的是把一个有多个参数的函数转换成一个只接收部分参数,其他参数由该函数里返回的函数接收的函数
10+
例:
11+
```
12+
function foo(x,y,z){
13+
console.log(x+y+z);
14+
}
15+
foo(2,3,4)
16+
17+
function bar1(x){
18+
return function(y){
19+
return function(z){
20+
return x+y+z
21+
}
22+
}
23+
}
24+
console.log(bar(2)(3)(4));
25+
26+
//可以写成箭头函数
27+
const bar2 = (x) =>{
28+
return (y)=>{
29+
return (z)=>{
30+
return x+y+z
31+
}
32+
}
33+
}
34+
35+
再简化
36+
const bar = (x) => (y) => (z) => x + y + z;
37+
38+
console.log(bar(2)(3)(4));
39+
```
40+
## 为什么要柯里化
41+
目的是让各个函数的职责变得单一
42+
## 实现一个柯里化的工具函数
43+
```
44+
function currying(fn) {
45+
//返回处理过的柯里化函数
46+
function curried1(...args1) {
47+
//当参数个数达到传入的参数个数,就执行
48+
if (args1.length >= fn.length) {
49+
return fn.apply(this,args1)
50+
}else{
51+
//没有达到,进行递归处理参数
52+
return function curried2(...args2){
53+
return curried1.apply(this,([...args1,...args2]))
54+
}
55+
}
56+
}
57+
return curried1;
58+
}
59+
60+
function add(x, y, z) {
61+
return x + y + z;
62+
}
63+
64+
let newCurrying = currying(add);
65+
66+
console.log(newCurrying(10)(20)(30));
67+
//输出60
68+
```
69+

data/blog/shousipromise(er).mdx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: 手撕promise(二)
3+
date: 2023-12-23T12:47:57Z
4+
summary:
5+
tags: []
6+
---
7+
8+
## 手撕promise
9+
```
10+
const STATUS_PENDING = "pending";
11+
const STATUS_RESOLVE = "fulfilled";
12+
const STATUS_REJECT = "rejected";
13+
14+
class Mypromise {
15+
constructor(executor) {
16+
this.status = STATUS_PENDING;
17+
this.value = null;
18+
this.reason = null;
19+
20+
const resolve = (value) => {
21+
if (this.status === STATUS_PENDING) {
22+
queueMicrotask(()=>{
23+
this.status === STATUS_RESOLVE;
24+
this.value = value;
25+
this.onFulfilled(this.value);
26+
})
27+
}
28+
};
29+
const reject = (value) => {
30+
if (this.status === STATUS_PENDING) {
31+
queueMicrotask(()=>{
32+
this.status === STATUS_REJECT;
33+
this.value = value;
34+
this.onReject(this.value);
35+
})
36+
}
37+
};
38+
executor(resolve, reject);
39+
}
40+
41+
then(onFulfilled, onReject) {
42+
this.onFulfilled = onFulfilled;
43+
this.onReject = onReject;
44+
}
45+
}
46+
47+
const promise = new Mypromise((resolve, reject) => {
48+
resolve("测试resolve")
49+
reject("测试reject")
50+
});
51+
52+
promise.then(
53+
(res) => {
54+
console.log(res);
55+
},
56+
(err) => {
57+
console.log(err);
58+
}
59+
);
60+
```
61+

0 commit comments

Comments
 (0)