From 4f53f9257a856a7b94e1ffe77eb71b531f46c843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Wed, 16 Nov 2022 00:46:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../this&\346\255\243\345\210\231.md" | 12 + ...022-11-16this&\346\255\243\345\210\231.md" | 285 ++++++++++++++++++ 2 files changed, 297 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/this&\346\255\243\345\210\231.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-16this&\346\255\243\345\210\231.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/this&\346\255\243\345\210\231.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/this&\346\255\243\345\210\231.md" new file mode 100644 index 0000000..32ffdc2 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/this&\346\255\243\345\210\231.md" @@ -0,0 +1,12 @@ +``` + +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-16this&\346\255\243\345\210\231.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-16this&\346\255\243\345\210\231.md" new file mode 100644 index 0000000..725dfe0 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-16this&\346\255\243\345\210\231.md" @@ -0,0 +1,285 @@ + 一,this指向 + +#### 1,概念 + +- this的意思为“这个;当前”,**是一个指针型变量,它动态指向当前函数的运行环境** + +#### 2,指向情况 + +- **优先级new 显示 >隐式>默认** +- **在全局范围内,this指向全局对象**(浏览器下指window对象) +- 对象函数调用时,this指向当前对象 +- 全局函数调用时,应该是指向调用全局函数的对象 +- 使用new关键字实例化对象时,this指向新创建的对象 +- 当用apply和call上下文调用的时候指向传入的第一个参数 + +### 二,直接调用 + +- 在函数被直接调用时,this 将指向全局对象。在浏览器环境中全局对象是 Window + +#### 1,全局作用域中:this永远指向window + +``` +console.log(this); +console.log(window === this);//true +``` + +#### 2,函数作用域中: + +- 如果函数直接被调用 this指向window 函数名() +- 被对象的对象.属性()调用 函数中的this指向这个对象 + +##### (1),严格模式下 + +``` +function fun(){ + 'use strict' + console.log(this) +} +fun();//undefined +``` + +##### (2),非严格模式下 + +``` +function fun(){ + console.log(this) +} +fun();//window +``` + +### 三,独立调用-(立即执行函数,闭包) + +- **this指向的都是window对象,比如:立即执行函数,闭包** + +#### 1,立即执行函数是独立调用 + +``` +var fun=(function () { + console.log(this); +}()); +``` + +#### 2,闭包 + +- **每个函数在执行时候都会生成一个独一无二的this, 且它们的指向可能是相同的** + +``` +function fn(){ + console.log(this); //window +function fun1(){ + console.log(this); //window + } + fun1(); +function fun2(){ + console.log(this); //window + } + fun2(); + } +fn() +``` + +### 四,隐式绑定 + +- **规则:谁调用,指向谁。比如,对象调用对象的方法** + +``` +var a = 0; +var obj = { + a: 2, +fn: function () { + console.log(this); //obj +function test() { + console.log(this); //window对象 + } + return test; + } +} +te(); +var te = obj.fn(); +te(); +te(); +``` + +#### 1,隐式丢失 + +``` +var a = 'hello'; +function fn(){ + console.log(this.a); + } +var obj = { + a:'obj', + fn:fn + } +obj.fn(); +var demo = obj.fn; +demo(); +``` + +### 五,显示绑定 (call,apply,bind) + +- call,apply,bind改变this指向 + +#### 1,bind— 原型中的方法Function.prototype.bind() + +``` +function fn(a){ + this.a = a + console.log(this.a); + } +var obj={ + a:'obj' + } +fn.call(obj,5); +fn.apply(obj,[5]) +var fn1 = fn.bind(obj)(5); +fn1; +``` + +##### (1),多次bind时只认第一次bind的值 + +``` +function fun(){ + console.log(this) +} +fun.bind(1).bind(2)();//1 +``` + +##### (2),bind函数中this不会被修改 + +``` +function fun(){ + console.log(this) +} +fun.bind(1); +fun +``` + +#### 2,父函数是有能力决定子函数的this 指向 + +``` +const arr = [1,2,3]; +arr.forEach( + function (e){ + console.log(this); + } +) +``` + +### 六,new绑定 + +- **当使用 new 关键字调用函数时,函数中的 this 一定是 JS 创建的新对象** + +``` +function GetName(){ + 'use strict' + this.a = 'ABC'; + console.log(this); //指代当前对象 + } +GetName(); //独立调用 +var name = new GetName(); +var name1 = new GetName(); +``` + +### 七,箭头函数 + +- **箭头函数this的指向不会发生改变,也就是说在创建箭头函数时就已经确定了它的this的指向了;它的指向永远指向箭头函数外层的 this** +- **箭头函数中this指向不会被call,apply修改** + +``` +var a=1,b=2; +var result = (a,b) => { +var sum = a + b; +return sum; +} +console.log(result(a,b)); +``` + +#### 1,如果形参时只有一个,可以省略圆括号 + +``` +var result = a => { +var sum = a ; +return sum; +} +console.log(result(a)); +``` + +#### 2,无参和多个参数,都需要使用圆括号 + +``` +var result = () => { +return 10; +} +console.log(result()); +``` + +#### 3,只有一条return语句时,可以省略大括号和return关键字 + +``` +var result = (a,b) => a+b; +console.log(result(a,b)); +``` + +#### 4,箭头函数会指向父级函数中的this + +``` +var a = 'window' +//父级 +var obj = { + a:'obj', + fn: () => this.a +} +console.log(obj.fn()); +//子集 +var circle = { + radius: 10, + outerDiameter() { + console.log(this); +var innerDiameter = () => { + console.log(2 * this.radius); + }; + innerDiameter(); + } + outerDiameter : () => {console.log(2*this.radius);} +}; + +circle.outerDiameter(); //20 +``` + +### 八,对象里两种函数书写形式 + +``` +fn(){ + console.log('11'); +} +``` + +或 + +``` +fn: function () { + console.log(this);; + } +} +obj.fn(); //obj +``` + +### 一,正则表达式 + +#### 1,概念 + +- 它是一种通用的工具,在 JavaScript、PHP、Java、Python、C++等几乎所有的编程语言中都能使用; + +#### 2.调用 + +- **==exec==** + +匹配成功: 返回一个数组 + +匹配失败: 返回null + +- ==**test**== + +测试待检测的字符串是或否能匹配到,匹配到返回true,否则返回false \ No newline at end of file -- Gitee From 5fb1b8660e774cd48f89c45c98eb8b5f40d174ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Thu, 17 Nov 2022 12:57:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\255\243\345\210\2312.md" | 21 ++++++ ...3\345\210\231&\345\274\202\345\270\270.md" | 70 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\255\243\345\210\2312.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-17\346\255\243\345\210\231&\345\274\202\345\270\270.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\255\243\345\210\2312.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\255\243\345\210\2312.md" new file mode 100644 index 0000000..a9091f9 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\255\243\345\210\2312.md" @@ -0,0 +1,21 @@ +``` + +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-17\346\255\243\345\210\231&\345\274\202\345\270\270.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-17\346\255\243\345\210\231&\345\274\202\345\270\270.md" new file mode 100644 index 0000000..f781936 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-11-17\346\255\243\345\210\231&\345\274\202\345\270\270.md" @@ -0,0 +1,70 @@ +分组和捕获 + +分组:1.改变优先级 2.分组引用 + +分组用()进行,为子表达式 + +捕获和反捕获都是针对子表达式进行的 + +``` +/(ab)(12)/.exec('ab12')//小括号中的为单独的字符串 +/d{4}-/d{3}-d{2} +//数字代表几位数 +regExp.$1(2.3.....)//输出下标为1的 +``` + +使用replace捕获分组 + +``` +var pattern=/([a-z]+)(\d+) +str.replace.(pattern."$2$1") +//与regExp中的$2$1作用相同 +``` + +反捕获 反捕获组只匹配结果,不能在表达式和程序中做进一步处理 举例说明 (hello) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到。 (?:hello) 匹配 pattern 但不获取匹配结果 (?=hello) 正向肯定预查(look ahead positive assert。这是一个非获取匹配。 (?!hello) 正向否定预查(negative assert)。这是一个非获取匹配。 (?<=hello) 反向(look behind)肯定预查,与正向肯定预查(?=pattern)类似,只是方向相反。这是一个非获取匹配。 (?反捕获 ??--> 非贪婪{0,1} + +``` + 查b (由c紧跟着) + console.log(/b(?=c)/.exec('abcbd')); + + 查a (后面跟的不是b) + console.log(/a(?!b)/.exec('abca')); +``` + +异常 + +null:不能再添加其他属性 + +包装类 :null.undefined没有包装类 + +异常处理 + +try(//尝试捕获异常,里面放的是可能出错的代码){ + +}catch(error){ + +log(error)//指出报错的地方 + +}finally//不管有没有捕获错误都会运行{ + +} + +每次只能捕获一个异常 \ No newline at end of file -- Gitee