Skip to content

Commit d47b357

Browse files
committed
JavaScript
1 parent bec089e commit d47b357

File tree

3 files changed

+85
-26
lines changed

3 files changed

+85
-26
lines changed

docs/_image/prototype.png

149 KB
Loading

docs/http/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ HTTP2 支持服务端推送
3434
</p>
3535
</details>
3636

37+
---
38+
#### http 和 https 的区别 ?
39+
40+
<details><summary><b>答案</b></summary>
41+
<p>
42+
43+
</p>
44+
</details>
45+
3746
---
3847

3948
#### CSRF(Cross-site request forgery)跨站请求伪造?

docs/javascript/README.md

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn(1, 2, 3)
295295

296296
---
297297

298-
#### JS 怎么实现一个类,怎么实例化这个类?
298+
#### JS ES5,ES6 怎么实现一个类,怎么实例化这个类?
299299

300300
<details><summary><b>答案</b></summary>
301301
<p>
@@ -331,7 +331,7 @@ let child = new Child('parent')
331331

332332
---
333333

334-
#### JS 怎么继承类?
334+
#### JS ES5,ES6 怎么继承类?
335335

336336
<details><summary><b>答案</b></summary>
337337
<p>
@@ -398,36 +398,30 @@ function Child() {
398398
}
399399
Child.prototype = Parent.prototype
400400
```
401-
402-
缺点:多执行了一次构造函数会 `Child3.prototype = new Parent()`
401+
通过上面的方法继承,尝试修改实例属性
403402

404403
```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)
412407
```
408+
缺点:子类实例的构造函数是Parent,显然这是不对的,应该是Child
413409

414410
```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]
423415
}
424-
class Dog extends Animal {
425-
construtor(type) {
426-
supper(type)
427-
}
416+
function Child(){
417+
Parent.call(this)
418+
this.type='child'
428419
}
420+
Child.prototype=Object.create(Parent.prototype)
429421
```
430422

423+
424+
431425
</p>
432426
</details>
433427

@@ -552,6 +546,32 @@ console.log(arr) // [1, 5, 10, 25, 40, 1000]
552546
</p>
553547
</details>
554548

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+
555575
---
556576

557577
#### 深拷贝和浅拷贝的区别,以及实现?
@@ -644,7 +664,7 @@ console.log(obj) // {username: "sunnie"}
644664

645665
```javascript
646666
var person = {
647-
value: 1,
667+
value: 1
648668
}
649669
function say(name, age) {
650670
console.log(name)
@@ -678,6 +698,17 @@ Function.prototype.call2 = function(context) {
678698
context.fn()
679699
delete context.fn
680700
}
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
681712
```
682713

683714
**apply 实现**
@@ -715,7 +746,7 @@ Function.prototype.bind2 = function(context) {
715746

716747
**`函数防抖`(debounce):防抖就是将一段时间内连续的多次触发转化为一次触发。**
717748

718-
比如:我点击一个按钮,`delay`设置 3s ,我手速超快,从来不让点击间隔时间大于 3s 函数就不执行,一旦大于了 3s 就执行了
749+
比如:我点击一个按钮,`delay`设置 3s ,我手速超快,从来不让点击间隔时间大于等于 3s 函数就不执行,一旦大于等于了 3s 就执行了
719750

720751
```javascript
721752
function debounce(func, delay) {
@@ -758,7 +789,7 @@ mouse move 时减少计算次数:`debounce`
758789
input 中输入文字自动发送 ajax 请求进行自动补全: `debounce`
759790
ajax 请求合并,不希望短时间内大量的请求被重复发送:`debounce`
760791
resize window 重新计算样式或布局:`debounce``throttle`
761-
scroll 时触发操作,如随动效果:throttle
792+
scroll 时触发操作,如随动效果:`throttle`
762793
对用户输入的验证,不想停止输入再进行验证,而是每 n 秒进行验证:`throttle`
763794

764795
</p>
@@ -796,6 +827,25 @@ scroll 时触发操作,如随动效果:throttle
796827

797828
---
798829

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+
---
799849
#### 前端模块化:`CommonJS`,`AMD`,`CMD`,`ES6`
800850

801851
<details><summary><b>答案</b></summary>

0 commit comments

Comments
 (0)