@@ -295,7 +295,7 @@ fn(1, 2, 3)
295
295
296
296
---
297
297
298
- #### JS 怎么实现一个类,怎么实例化这个类?
298
+ #### JS ES5,ES6 怎么实现一个类,怎么实例化这个类?
299
299
300
300
<details ><summary ><b >答案</b ></summary >
301
301
<p >
@@ -331,7 +331,7 @@ let child = new Child('parent')
331
331
332
332
---
333
333
334
- #### JS 怎么继承类?
334
+ #### JS ES5,ES6 怎么继承类?
335
335
336
336
<details ><summary ><b >答案</b ></summary >
337
337
<p >
@@ -398,36 +398,30 @@ function Child() {
398
398
}
399
399
Child .prototype = Parent .prototype
400
400
```
401
-
402
- 缺点:多执行了一次构造函数会 ` Child3.prototype = new Parent() `
401
+ 通过上面的方法继承,尝试修改实例属性
403
402
404
403
``` javascript
405
- // 只实现了部分继承 ,prototype上的没有被继承
406
- function Animal (type ) {
407
- this .type = type
408
- }
409
- function Dog () {
410
- Animal .call (this )
411
- }
404
+ var s1 = new Child ()
405
+ var s2 = new Child ()
406
+ console .log (s1 .constructor )
412
407
```
408
+ 缺点:子类实例的构造函数是Parent,显然这是不对的,应该是Child
413
409
414
410
``` javascript
415
- // ES6 实现继承
416
- class Animal {
417
- construtor (type ) {
418
- this .type = type
419
- }
420
- eat () {
421
- console .log (' eat' )
422
- }
411
+
412
+ function Parent (){
413
+ this .name = ' parent'
414
+ this .play = [1 ,2 ,3 ]
423
415
}
424
- class Dog extends Animal {
425
- construtor (type ) {
426
- supper (type)
427
- }
416
+ function Child (){
417
+ Parent .call (this )
418
+ this .type = ' child'
428
419
}
420
+ Child .prototype = Object .create (Parent .prototype )
429
421
```
430
422
423
+
424
+
431
425
</p >
432
426
</details >
433
427
@@ -552,6 +546,32 @@ console.log(arr) // [1, 5, 10, 25, 40, 1000]
552
546
</p >
553
547
</details >
554
548
549
+ ---
550
+ #### js 请用` ES5 ` ` ES6 ` 合并数组?
551
+
552
+ 知识点:` 合并数组 `
553
+
554
+ <details ><summary ><b >答案</b ></summary >
555
+ <p >
556
+
557
+ ``` javascript
558
+
559
+ let a = [1 , 2 , 3 ]
560
+ let b = [4 , 5 , 6 ]
561
+
562
+ // ES5 方法一
563
+ a .concat (b)
564
+ // ES5 方法二
565
+ Array .prototype .push .apply (a, b)
566
+ console .log (a) // [1,2,3,4,5,6]
567
+ // ES6
568
+ let c = [... a, ... b]
569
+ console .log (c)
570
+
571
+ ```
572
+ </p >
573
+ </details >
574
+
555
575
---
556
576
557
577
#### 深拷贝和浅拷贝的区别,以及实现?
@@ -644,7 +664,7 @@ console.log(obj) // {username: "sunnie"}
644
664
645
665
``` javascript
646
666
var person = {
647
- value: 1 ,
667
+ value: 1
648
668
}
649
669
function say (name , age ) {
650
670
console .log (name)
@@ -678,6 +698,17 @@ Function.prototype.call2 = function(context) {
678
698
context .fn ()
679
699
delete context .fn
680
700
}
701
+
702
+ // 测试一下
703
+ var foo = {
704
+ value: 1
705
+ };
706
+
707
+ function bar () {
708
+ console .log (this .value );
709
+ }
710
+
711
+ bar .call2 (foo); // 1
681
712
```
682
713
683
714
** apply 实现**
@@ -715,7 +746,7 @@ Function.prototype.bind2 = function(context) {
715
746
716
747
** ` 函数防抖 ` (debounce):防抖就是将一段时间内连续的多次触发转化为一次触发。**
717
748
718
- 比如:我点击一个按钮,` delay ` 设置 3s ,我手速超快,从来不让点击间隔时间大于 3s 函数就不执行,一旦大于了 3s 就执行了
749
+ 比如:我点击一个按钮,` delay ` 设置 3s ,我手速超快,从来不让点击间隔时间大于等于 3s 函数就不执行,一旦大于等于了 3s 就执行了
719
750
720
751
``` javascript
721
752
function debounce (func , delay ) {
@@ -758,7 +789,7 @@ mouse move 时减少计算次数:`debounce`
758
789
input 中输入文字自动发送 ajax 请求进行自动补全: ` debounce `
759
790
ajax 请求合并,不希望短时间内大量的请求被重复发送:` debounce `
760
791
resize window 重新计算样式或布局:` debounce ` 或 ` throttle `
761
- scroll 时触发操作,如随动效果:throttle
792
+ scroll 时触发操作,如随动效果:` throttle `
762
793
对用户输入的验证,不想停止输入再进行验证,而是每 n 秒进行验证:` throttle `
763
794
764
795
</p >
@@ -796,6 +827,25 @@ scroll 时触发操作,如随动效果:throttle
796
827
797
828
---
798
829
830
+ #### js 原型链
831
+
832
+ 知识点:` 原型链 `
833
+
834
+ <details ><summary ><b >答案</b ></summary >
835
+ <p >
836
+
837
+ 函数都有 ` prototype ` 属性,对象没有 ` prototype ` 属性
838
+ 一个函数通过 ` new ` 称为` 实例 ` ,这个函数称为 ` 构造函数 `
839
+ ` prototype ` 原型对象
840
+
841
+ ![ prototype] ( ../_image/prototype.png )
842
+
843
+
844
+
845
+ </p >
846
+ </details >
847
+
848
+ ---
799
849
#### 前端模块化:` CommonJS ` ,` AMD ` ,` CMD ` ,` ES6 `
800
850
801
851
<details ><summary ><b >答案</b ></summary >
0 commit comments