From b3c8281ecd63a3d51c1caa8e9e25ba7e0f965751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=96=87=E9=BE=99?= <2717752887@qq.com> Date: Thu, 3 Nov 2022 03:44:37 +0000 Subject: [PATCH] =?UTF-8?q?2022-11.3=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴文龙 <2717752887@qq.com> --- ...00\346\254\241\347\254\224\350\256\260.md" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-11.3\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" diff --git "a/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-11.3\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" "b/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-11.3\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..f8801ef --- /dev/null +++ "b/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-11.3\347\254\254\345\215\201\344\270\200\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,81 @@ +# 函数 + +###### call,apply,bind + +改变this指向,函数被执行时,内部会产生一个this + +arguments:伪数组,只有下标,长度,元素是实参的映射 + +实列:(call apply 两者类似) + +``` +function Person(name, age) { + + var { name : '张三' ,age:18} + + console.log(this); + + this.name = name; //obj.name = name; + + this.age = age; //obj.age = age; + + return {} + + } + + var obj = {}; + +Person.call(obj,'张三',18); +Person.apply(obj, ['张三', 18]); +Person('张三',18); +``` + +实列:bind + +``` + // const module = { + // x: 42, + // getX: function () { + // console.log(this); + // return this.x; //window.x + // } + // }; + + // var x = 99; + //bind + // const unboundGetX = module.getX; + // console.log(unboundGetX()); //42 + // const boundGetX = unboundGetX.bind(module);//函数 调用bind方法,返回一个带this指向的新函数 + // console.log(boundGetX()); +``` + +#### 严格模式('use strict') + +:严格模式:防止意外发生错误时,及时抛出该错误 + +1.在严格模式下,函数内部不会再意外地生成全局变量,而是抛出异常 + +2.严格模式要求一个对象内的所有属性名在对象内必须唯一 + +3.严格模式下会抛出错误,非严格模式会自动转换全局 + +4.构造函数使用严格模式,必须使用new: + +``` + // function Product(name, price) { + // 'use strict' + // this.name = name; + // this.price = price; + // } + // new Product('张三',18); +``` + +函数命名 + + //构造函数:Person() + +​ //普通函数小驼峰: getNameAge + +​ //普通变量: personAge perons_age + +​ //常量:PI \ No newline at end of file -- Gitee