Skip to content

Commit a8e1234

Browse files
committed
add: 面试总结
1 parent 841cd40 commit a8e1234

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5996
-68
lines changed

ES6/Arrary/array.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Tiny
33
* @Date: 2019-03-01 10:05:41
44
* @Last Modified by: [email protected]
5-
* @Last Modified time: 2019-03-01 18:01:32
5+
* @Last Modified time: yyyy-10-Mo 04:40:54
66
*/
77

88
/**
@@ -135,8 +135,12 @@ console.log(typesOf(null, [], NaN, undefined, new Map())) // [ 'object', 'object
135135
*
136136
*/
137137
console.log(Array()) // []
138+
// 一个参数时是指数组长度
138139
console.log(Array(3)) // [ <3 empty items> ]
140+
// 不少于两个参数时才会返回所有参数组成的新数组
139141
console.log(Array(3, 11, 8)) // [ 3, 11, 8 ]
142+
143+
// Array.of()解决了Array()行为不统一
140144
console.log(Array.of()) // []
141145
console.log(Array.of(3)) // [ 3 ]
142146
console.log(Array.of(3, 11, 8)) // [ 3, 11, 8 ]

ES6/Generator/generator.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* @Author: Tiny
33
* @Date: 2019-02-25 14:10:56
4-
* @Last Modified by: tiny.jiao
5-
* @Last Modified time: 2019-02-25 22:20:42
4+
* @Last Modified by: tiny.jiao@aliyun.com
5+
* @Last Modified time: yyyy-10-Th 10:09:43
66
*/
77

88
/**
@@ -29,13 +29,18 @@
2929
* 10: 作为对象属性的Generator函数
3030
* 11: Generator 函数的this
3131
*/
32+
function test333 () {
33+
console.log(22);
34+
return Promise.resolve("ok");
35+
};
3236
function* helloworldGenerator() {
33-
yield 'hello'
34-
yield 'world'
35-
return 'ending'
37+
yield test333();
38+
yield 'hello';
39+
yield 'world';
40+
return 'ending';
3641
}
3742
const hw = helloworldGenerator()
38-
console.log(hw) // Object [Generator] {}
43+
// console.log(hw) // Object [Generator] {}
3944
console.log(hw.next()) // { value: 'hello', done: false }
4045
console.log(hw.next()) // { value: 'world', done: false }
4146
console.log(hw.next()) // { value: 'ending', done: false }

ES6/IteratorForof/iterator.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
*/
2222
// 下面是模拟next方法返回值的例子
2323
function makeIterator(arr) {
24-
let nextIndex = 0
24+
let nextIndex = 0;
2525
return {
2626
next: function() {
27-
return nextIndex < arr.length ? { value: arr[nextIndex++], done: false } : { value: undefined, done: true }
27+
return nextIndex < arr.length
28+
? { value: arr[nextIndex++], done: false }
29+
: { value: undefined, done: true };
2830
}
29-
}
31+
};
3032
}
3133
const it = makeIterator(['a', 'b'])
3234
console.log(it.next()) // { value: 'a', done: false }

ES6/Reflect/reflect.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Tiny
33
* @Date: 2019-02-22 10:22:31
44
* @Last Modified by: [email protected]
5-
* @Last Modified time: 2019-02-22 14:07:36
5+
* @Last Modified time: yyyy-10-Su 05:47:58
66
*/
77

88
/**
@@ -15,7 +15,7 @@
1515
* 3:让Object操作都变成函数行为
1616
* 如:name in obj 用 Reflect.has(obj, name)替代
1717
* delete obj[name] 用 Reflect.deleteProperty(obj, name)替代
18-
* 4:Reflect对象的方法与Proxy对象的方法一一对应,只要时Proxy对象的方法就能在Reflect对象上找打对应的方法
18+
* 4:Reflect对象的方法与Proxy对象的方法一一对应,只要是Proxy对象的方法就能在Reflect对象上找到对应的方法
1919
* 3: Reflect对象总共的13个静态方法
2020
* 1: Reflect.apply(target, thisArg, args)
2121
* 2: Reflect.construct(target, args)
@@ -100,7 +100,8 @@ function Greeting(name) {
100100
// new 的写法
101101
const instance = new Greeting('tiny')
102102
// Reflect.construct 的写法
103-
const instance2 = Reflect.construct(Greeting, ['tiny'])
103+
const instance2 = Reflect.construct(Greeting, ['tiny']);
104+
console.log(instance2.name);
104105

105106
/**
106107
* Reflect.getPrototypeOf(obj): 用于读取对象的__proto__属性,
@@ -113,6 +114,7 @@ console.log(Reflect.getPrototypeOf(myObject3) === Greeting.prototype) // true
113114
const myObject4 = {}
114115
Object.setPrototypeOf(myObject4, Array.prototype)
115116
Reflect.setPrototypeOf(myObject4, Array.prototype)
117+
console.log(myObject4.prototype);
116118

117119
/**
118120
* Reflect.ownKeys(target):

ES6/class/class.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ const notPerson = Person.call(this, 'san zhang') // 报错
4848
// Class内部调用new.target,返回当前的Class
4949
class Reactangle {
5050
constructor(length, width) {
51-
console.log(new.target === Reactangle) // true
5251
this.length = length
5352
this.width = width
53+
console.log(new.target === Reactangle, this.length, this.width); // true
5454
}
5555
}
56-
const obj = new Reactangle(4, 3)
56+
const obj = new Reactangle(4, 3);
5757

5858
// 子类继承父类时,new.target会返回子类
5959
class Father {

ES6/class/extendsOfClass.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,35 @@ class ColorPoint extends Point {
3232
}
3333
}
3434

35-
// ColorPoint的构造方法没有调用super方法,固新建实列时报错
36-
class Point {
35+
// ColorPoint的构造方法没有调用super方法,故新建实列时报错
36+
class Point2 {
3737
}
38-
class ColorPoint extends Point {
38+
class ColorPoint2 extends Point2 {
3939
constructor() {
4040
}
4141
}
42-
const cp = new ColorPoint() // ReferenceError
42+
// Must call super constructor in derived class before accessing 'this' or returning from derived constructor
43+
const cp = new ColorPoint2() // ReferenceError
4344

4445
// 子类的构造方法,如果没有显示定义,会被默认添加
45-
class ColorPoint extends Point {
46+
class ColorPoint3 extends Point {
4647

4748
}
4849
// 等同于
49-
class ColorPoint extends Point {
50+
class ColorPoint4 extends Point {
5051
constructor(...args) {
5152
super(...args)
5253
}
5354
}
5455

5556
// 在子类构造函数中,只有调用super之后才可以使用this关键字,否则会报错
56-
class Point {
57+
class Point5 {
5758
constructor(x, y) {
5859
this.x = x
5960
this.y = y
6061
}
6162
}
62-
class ColorPoint extends Point {
63+
class ColorPoint5 extends Point {
6364
constructor(x, y, color) {
6465

6566
// 在super调用之前使用this,所以报错

ES6/let-const.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: Tiny
3+
* @Date: 2020-03-16 16:49:13
4+
* @Last Modified by: [email protected]
5+
* @Last Modified time: 2020-03-16 16:53:22
6+
*/
7+
8+
/**
9+
* let, const
10+
* 注意: const声明不允许修改绑定,但允许修改值
11+
*/
12+
const data = {
13+
value: 1
14+
}
15+
data.value = 2
16+
data.num = 3
17+
18+
console.log(data.value)
19+
// data = {}
20+
21+
/**
22+
* 最佳实践:
23+
* 然而另一种做法日益普及:默认使用 const,只有当确实需要改变变量的值的时候才使用 let。这是因为大部分的变量的值在初始化后不应再改变,
24+
* 而预料之外的变量之的改变是很多 bug 的源头。
25+
*/

ES6/proxy/proxy.html

+129-7
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,136 @@
3030
}
3131
})
3232
const el = dom.div({id: 'body', class: 'body'},
33-
'hello, my name is ',
34-
dom.ul({class: 'nameList'},
35-
dom.li({class: 'item'}, 'the web'),
36-
dom.li({class: 'item'}, 'food'),
37-
dom.li({class: 'item'}, '...actualy that\'s it'),
38-
)
33+
'hello, my name is ',
34+
dom.ul({class: 'nameList'},
35+
dom.li({class: 'item'}, 'the web'),
36+
dom.li({class: 'item'}, 'food'),
37+
dom.li({class: 'item'}, '...actualy that\'s it'),
38+
39+
),
40+
dom.div({class: 'artical'},
41+
dom.div({class: 'left'}, 'article left'),
42+
dom.div({class: 'right'}, 'article right'),
43+
dom.button({class: 'proxy', onclick: "handleClick"}, 'proxy test')
44+
)
3945
)
40-
document.body.appendChild(el)
46+
document.body.appendChild(el);
47+
48+
function observerProxy(obj) {
49+
const handler = {
50+
get(target, key, receiver) {
51+
if (typeof target[key] === "object" && target[key] !== null) {
52+
return new Proxy(target[key], handler)
53+
}
54+
return Reflect.get(target, key, receiver);
55+
},
56+
set(target, key, value, receiver) {
57+
console.log(key + " data is changed !")
58+
return Reflect.set(target, key, value, receiver);
59+
}
60+
};
61+
return new Proxy(obj, handler);
62+
}
63+
const obj = {
64+
name: 'hould on',
65+
flag: {
66+
book: ["javascript 高程第四版本", "js精髓"],
67+
user: {
68+
name: 'tiny',
69+
age: 18,
70+
height: 180,
71+
weight: 70
72+
}
73+
}
74+
}
75+
const objTest = observerProxy(obj);
76+
77+
78+
/**
79+
* Object.defineProperty()缺点:
80+
* 1: 新加的属性不能监听
81+
* 2: 数组变化不能监听
82+
* 3: 需要使用递归便利每一个key
83+
* 4: 直接修改原始数据
84+
* 5: 代理的是属性
85+
*/
86+
function observer(obj) {
87+
if (typeof obj === "object" && obj !== null) {
88+
for (const key in obj) {
89+
if (obj.hasOwnProperty(key)) {
90+
const value = obj[key];
91+
defineReactive(obj, key, value)
92+
}
93+
}
94+
}
95+
}
96+
function defineReactive(obj, key, value) {
97+
observer(value);
98+
Object.defineProperty(obj, key, {
99+
get() {
100+
return value;
101+
},
102+
set(val) {
103+
observer(val);
104+
console.log(key + " data is changed !");
105+
value = val;
106+
}
107+
})
108+
}
109+
const objDefin = observer(obj);
110+
111+
/**
112+
* Proxy:
113+
* 1: 代理的是对象
114+
* 2: 可以拦截到数组的变化
115+
* 3: 拦截的方法多大13中
116+
* 4: 返回一个拦截后的数组
117+
*
118+
*/
119+
// proxy 使用场景
120+
// 1: 数组负数索引
121+
const arrayProxy = {
122+
get(target, key, receiver) {
123+
const _index = key < 0 ? target.length + Number(key) : key;
124+
return Reflect.get(target, _index, receiver);
125+
}
126+
};
127+
const arrV = [1, 2, 3, 4, 5];
128+
const arr = new Proxy(arrV, arrayProxy)
129+
arr[-4] // 2
130+
131+
// 2: 表单校验
132+
const formValidator = {
133+
set(target, key, value, receiver) {
134+
if (key === "age") {
135+
if (value < 0 || !Number.isInteger(value)) {
136+
throw new TypeError("please input valide value!")
137+
}
138+
}
139+
return Reflect.set(target, key, value, receiver);
140+
}
141+
};
142+
const objV = {
143+
age: 16
144+
};
145+
const obj2 = new Proxy(objV, formValidator)
146+
147+
function validator () {
148+
149+
}
150+
// 3. 数据格式化
151+
const formatData = {
152+
set(target, key, value, receiver) {
153+
if (key === "date") {
154+
Reflect.set(target, "timeStamp", +new Date(value), receiver)
155+
}
156+
return Reflect.set(target, key, value, receiver);
157+
}
158+
}
159+
const timeObj = { date: "" }
160+
const formatProxy = new Proxy(timeObj, formatData);
161+
formatProxy.date = "2020-11-23";
162+
console.log(formatProxy)
41163
</script>
42164
</body>
43165
</html>

ES6/proxy/proxy.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Tiny
33
* @Date: 2019-02-20 15:51:54
44
* @Last Modified by: [email protected]
5-
* @Last Modified time: 2019-02-21 18:37:51
5+
* @Last Modified time: yyyy-10-Su 05:40:11
66
*/
77

88
/**
@@ -44,6 +44,7 @@ let proxy = new Proxy(person, {
4444
}
4545
})
4646
console.log(proxy.name) // 张三
47+
person.age = 23
4748
console.log(proxy.age) // ReferenceError: Property "age" does not exist.
4849

4950
/**
@@ -66,7 +67,8 @@ function createArray(...e) {
6667
return new Proxy(target, handler)
6768
}
6869
let arr = createArray('a', 'b', 'c')
69-
console.log(arr[-2]) // b
70+
console.log(arr[1]) // b
71+
console.log(arr[-1]) // a
7072

7173
// 下面是利用get拦截实现一个生成各种DOM节点的通用函数dom
7274
const dom = new Proxy({}, {

ES6/proxy/revocable.js

-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@
44
* @Last Modified by: tiny.jiao
55
* @Last Modified time: 2019-02-22 23:23:15
66
*/
7-
/*
8-
* @Author: tiny.jiao
9-
* @Date: 2019-02-22 23:22:01
10-
* @Last Modified by: tiny.jiao
11-
* @Last Modified time: 2019-02-22 23:22:01
12-
*/

ES6/setMap/map.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Tiny
33
* @Date: 2019-02-19 14:13:16
44
* @Last Modified by: [email protected]
5-
* @Last Modified time: 2019-02-19 17:29:01
5+
* @Last Modified time: yyyy-10-Mo 01:16:44
66
*/
77

88
/**
@@ -48,6 +48,7 @@ function map() {
4848

4949
// Map转为对象
5050
function mapToObj(strMap) {
51+
// 这种方法创建的对象没有原型
5152
let obj = Object.create(null)
5253

5354
for (let [k, v] of strMap) {

0 commit comments

Comments
 (0)