diff --git "a/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\344\275\234\344\270\232.html" "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\344\275\234\344\270\232.html" new file mode 100644 index 0000000000000000000000000000000000000000..7f26f99e018790c8710e3a3e26a885ba46ef175a --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\344\275\234\344\270\232.html" @@ -0,0 +1,45 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git "a/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1103\345\216\237\345\236\213\344\275\234\344\270\232.html" "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1103\345\216\237\345\236\213\344\275\234\344\270\232.html" new file mode 100644 index 0000000000000000000000000000000000000000..a0e3386fd7da023aadb522de9a80b93a5b456550 --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1103\345\216\237\345\236\213\344\275\234\344\270\232.html" @@ -0,0 +1,38 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git "a/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1104\345\216\237\345\236\213\351\223\276and\347\273\247\346\211\277\344\275\234\344\270\232.html" "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1104\345\216\237\345\236\213\351\223\276and\347\273\247\346\211\277\344\275\234\344\270\232.html" new file mode 100644 index 0000000000000000000000000000000000000000..2d83020eb8728376a0164ecdd4793aeae626b5ba --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\344\275\234\344\270\232/2022-1104\345\216\237\345\236\213\351\223\276and\347\273\247\346\211\277\344\275\234\344\270\232.html" @@ -0,0 +1,129 @@ + + + + + + + Document + + + + + + \ No newline at end of file diff --git "a/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\345\222\214\351\200\222\345\275\222\347\254\224\350\256\260.md" "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\345\222\214\351\200\222\345\275\222\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..c76f66d2bca11f156dfa7d63bec34920f5a6b9e8 --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1102\344\270\245\346\240\274\345\207\275\346\225\260\345\222\214\351\200\222\345\275\222\347\254\224\350\256\260.md" @@ -0,0 +1,156 @@ +# 严格函数和递归 + +## 一、严格模式的使用 + +**严格模式** 的使用很简单,只有在代码首部加入字符串 “use strict”。有两种应用场景,一种是全局模式,一种是局部模式。 + +**1)全局模式** + +```js +'use strict' +//code +``` + +**2)局部模式** + +将”use strict”放到函数内的第一行,如下 + +```js +function() { + "use strict"; + //code +} +``` + +**call/apply的第一个参数直接传入不包装为对象** + +```js +'use strict' +function func() { + console.log(typeof this) +} +func.call('abcd') // string +func.apply(1) // number +``` + +依次为”string”,”number”。而在非严格模式中call/apply将对值类型的”abcd”,1包装为对象后传入,即两次输出都为”object”。 + +**call/apply的第一个参数为null/undefined时,this为null/undefined** 这里以call来示例 + +```js +'use strict' +function func() { + console.log(this) +} +func.call(undefined) // undefined +func.call(null) // null +``` + +依次是undefined,null。而非严格模式中则是宿主对象,浏览器里是window,node.js环境则是global。 + +**bind的第一个参数为null/undefined时,this为null/undefined** bind是ES5给Function.prototype新增的一个方法,它和call/apply一样在function上直接调用。它返回一个指定了上下文和参数的函数。当它的第一个参数为null/undefined时,情形和call/apply一样,this也为null/undefined。** + +```js +'use strict' +function func() { + console.log(this) +} +var f1 = func.bind(null) +var f2 = func.bind(undefined) +f1() // null +f2() // undefined +``` + +而在非严格模式中输出的都是window(或global)。 + +## “use strict” 的位置必须在首部。首部指其前面没有任何有效js代码。以下都是无效的,将不会触发严格模式。 + +a)“use strict” 前有代码, **无效** + +```js +var width = 10 +'use strict' +g = 100 +``` + +b)“use strict” 前有个空语句,**无效** + +```js +;//这里是空语句 +'use strict' +g = 100 +``` + + + +```js +function func() { + ; + 'use strict' + g = 200 +} +``` + + + +```js +function func() { + ;'use strict' + localVar = 200 +} +``` + +**当然,“use strict”前加注释是可以的** + +```js +// strict mode +'use strict' +g = 100 + +function func() { + // strict mode + 'use strict' + g = 200 +} +func() +``` + + + +## 二、什么是递归 + +递归是一把双刃剑,它是一种自己调用自己的算法,关键在于找到它的出口和入口,否则将导致导致堆栈溢出,陷入一种死循环的状况。许多 FP 语言不使用循环;他们只使用递归。递归的思想在于将一个未知的问题转变成一个已经解决的问题来实现。 + +### 1.递归的关键 + +- 将递归函数写好 +- 寻找递推关系 +- 找到递归出口条件 +- 根据条件和关系形成递推函数 + +### 2.头递归和尾递归 + +```js +如果递归有在函数调用后要执行的代码语句,则它是头递归。头递归通常很难转换为循环语句 + function head(n){ + if(n==0){ + return 0 + } + head(n-1) + console.log(n); + } + +尾递归在函数调用之后不会有任何代码语句,通常在函数声明的末尾。尾递归很容易转换成循环语句。 + function last(n){ + if(n==0){ + return 0 + } + console.log(n); + last(n-1) + + } + +``` + +**一般来说,尾递归会好一些。尽管它们都具有相同的时间复杂度和空间复杂度,但尾递归在函数堆栈中的内存方面更具优势。头递归将在函数堆栈内存中等待,直到执行后才会递归代码语句,这会导致执行结果整体延迟,而尾递归则直接在函数堆栈中终止执行。** + diff --git "a/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1103\345\216\237\345\236\213\347\254\224\350\256\260.md" "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1103\345\216\237\345\236\213\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..b7349e44797e8dbdd95afcf21e7227d1606629bd --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1103\345\216\237\345\236\213\347\254\224\350\256\260.md" @@ -0,0 +1,45 @@ +## 原型(prototype) + +```js +function Computer(brand,os) { + this.brand = brand; + this.os = os; + } + var computer1 = new Computer('Lenovo','Intel 5'); + var computer2 = new Computer('Mac','M2'); + Computer.prototype.screen = '18:9'; + +//打印,可以看到,属性prototype中仍然是一个对象 +console.log(function.prototype) +``` + +prototype:定义构造函数构造出的每个对象的ancestor。 + +**1.所有被该构造函数构造出的对象都可以继承原型上的属性和方法**。 + +**2.如果this有自己的属性,那么会覆盖掉原型上已有的属性。**(开发惯例:所有方法写到原型,部分属性写到this)一般来说属性是配置项,需要你主动传参去配置。但是一般来说某个插件的方法功能几乎是一致的。 + +### **与原型有关的几个方法** + +#### 1.prototype属性 + +prototype 存在于构造函数中 (其实任意函数中都有,只是不是构造函数的时候prototype我们不关注而已) ,他指向了这个构造函数的原型对象。 + +__prototype__:所有对象都有原型,包括prototype本身 + +#### 2.constructor属性 + + constructor属性存在于原型对象中,他指向了构造函数 + +```js + function students () { + } + alert(students.prototype.constructor === students); // true +``` + +我们根据需要,可以students.prototype 属性指定新的对象,来作为students的原型对象。但是这个时候有个问题,新的对象的constructor属性则不再指向students构造函数了。 + +#### 3.__proto__属性(注意:左右各是2个下划线) + + 用构造方法创建一个新的对象之后,这个对象中默认会有一个属性__proto__, 这个属性就指向了构造方法的原型对象。 + diff --git "a/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1104\345\216\237\345\236\213\351\223\276&\347\273\247\346\211\277\347\254\224\350\256\260.md" "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1104\345\216\237\345\236\213\351\223\276&\347\273\247\346\211\277\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..25bfba74ecbb1b34167d3961f3c4da99d504829c --- /dev/null +++ "b/28\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260/2022-1104\345\216\237\345\236\213\351\223\276&\347\273\247\346\211\277\347\254\224\350\256\260.md" @@ -0,0 +1,85 @@ +# 原型链&继承 + +### 什么是原型链? + +#### 原型链是**原型对象创建过程的历史记录,当访问一个对象的某个属性时,会先在这个对象本身属性上查找**。 + +## 1.原型链继承 + +通过实例化一个新的函数,子类的原型指向了父类的实例,子类就可以调用其父类原型对象上的私有属性和公有方法。(本质就是重写了原型对象) + +```js +function Parent() { + this.parentName = '父类'; +} +Parent.prototype.getParentName = function() { + return this.parentName; +}; + +function Child() { + this.childName = '子类'; +} +Child.prototype = new Parent(); +Child.prototype.getChildName = function() { + return this.childName +}; + +var c = new Child(); +console.log(c.getParentName()); // '父类' +``` + +### 2.构造函数继承 + +使用父类的构造函数来增强子类**实例**,等同于复制父类的实例给子类(不使用原型) + +```js +function SuperType(){ + this.color=["red","green","blue"]; +} +function SubType(){ + //继承自SuperType + SuperType.call(this); +} +var instance1 = new SubType(); +instance1.color.push("black"); +alert(instance1.color);//"red,green,blue,black" + +var instance2 = new SubType(); +alert(instance2.color);//"red,green,blue" +``` + +### 3.圣杯继承 + +原理:通过小原型链,Son里加东西不会再影响Father的proto + +```javascript +Father.prototype + + function F(){} //中间层 + + F.prototype = Father.protype + + Son.protype = new F();//依然能继承Father.prototype复制代码 +``` + +写一个继承 + +```js +function inherit(Target,Origin){ + +function F(){}; + +F.prototype = Origin.prototype; +Target.prototype = new F(); //两者位置不能颠倒,否则new时指向原来的原型 +Target.prototype.constructor = target; //constructor归位 +} + +Father.prototype.lastName="Su"; + +function Father(){} +function Son(){} + +inherit(Son,Father); +var son = newSon(); +var father = newFather(); +``` \ No newline at end of file