diff --git "a/12 \351\231\210\345\215\216\344\274\237/\347\254\224\350\256\260/2022-10-28 \351\242\204\347\274\226\350\257\221.md" "b/12 \351\231\210\345\215\216\344\274\237/\347\254\224\350\256\260/2022-10-28 \351\242\204\347\274\226\350\257\221.md" new file mode 100644 index 0000000000000000000000000000000000000000..a4f4bed4104e0c5318a65376f7ea8fd90cf8628c --- /dev/null +++ "b/12 \351\231\210\345\215\216\344\274\237/\347\254\224\350\256\260/2022-10-28 \351\242\204\347\274\226\350\257\221.md" @@ -0,0 +1,274 @@ +# 2022-10-26 + +## assign + +1.Object.assign方法用于对象的合并,将源对象( source )的所有可[枚举](https://so.csdn.net/so/search?q=枚举&spm=1001.2101.3001.7020)属性,复制到目标对象( target )。 + +```javascript +var target = { a: 1 }; +var source1 = { b: 2 }; +var source2 = { c: 3 }; +Object.assign(target, source1, source2); +target // {a:1, b:2, c:3} +``` + +3.Object.assign方法的第一个参数是目标对象,后面的参数都是源对象。 + +注意,如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性。 + +```javascript +var target = { a: 1, b: 1 }; +var source1 = { b: 2, c: 2 }; +var source2 = { c: 3 }; +Object.assign(target, source1, source2); +target // {a:1, b:2, c:3} +``` + +3.由于undefined和null无法转成对象,所以如果它们作为参数,就会报错。 + +```javascript +Object.assign(undefined) // 报错 +Object.assign(null) // 报错 +``` + +4.如果只有一个参数,Object.assign会直接返回该参数。 + +```javascript +var obj = {a: 1}; +Object.assign(obj) === obj // true +``` + +5.如果该参数不是对象,则会先转成对象,然后返回。 + +```javascript +typeof Object.assign(2) // "object" +``` + +Object.assign方法的第一个参数是目标对象,后面的参数都是源对象。 + +注意,如果目标对象与源对象有同名属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性。 + +## 包装类 + +1. 原始数据类型: number, boolean, string, null, undefined ,es6新加的(symbol,bigint) +2. JS 提供了 三个 特殊的引用类型:[Boolean](https://so.csdn.net/so/search?q=Boolean&spm=1001.2101.3001.7020)、Number、String(**undefined**和**null**没有自己的包装类) +3. null,undefined是没有包装类的 +4. 所有的包装类都有 valueOf() 和 toString() 方法,还有charAt(), [indexOf](https://so.csdn.net/so/search?q=indexOf&spm=1001.2101.3001.7020)()等方法。 + +JS为我们提供了三个包装类,通过这三个包装类可以将基本数据类型的数据转换为对象 + +```js +String() +    - 可以基本数据类型字符串转换为String对象 + +  Number() +    - 可以基本数据类型数字转换为Number对象 + +  Boolean() +    - 可以基本数据类型布尔值转换为Boolean对象 +--------------------------------------------- + 创建基本数据类型的对象 +  var str = new String('hello')  // str是一个值为'hello'的对象 即:String{'hello'} +  var str1 = 'hello' +  var num = new Number(3)  // num是一个值为3的对象 即:String{'hello'} +  var num1 = 3 +  var bool = new Boolean(true)  // bool是一个值为true的对象 即:String{'hello'} +  var bool2 = true +  console.log(str)  // String {'hello'} +  console.log(num)  // Number {3} +  console.log(bool)  // Boolean {true} +  console.log(typeof str)  // object +  console.log(str === str1)  // false  (str是对象,而str1是字符串) +---------------------------------------------------- +算出字符串的所占用字节的长度, +如: str ='live' 长度为4 str='live生活' 长度为8 +(提示:unicode<=255的占1字节,unicode>255的占用2字节, str.charCodeAt(i)查看i的字节码) + +var str = 'Live哈哈哈' +var count = 0 +for (let index = 0; index < str.length; index++) { +str.charCodeAt(index)>255 ? count+=2 : count++ +``` + +# 2022-10-27 + +## map和set + +Map 对象存有键值对,其中的键可以是任何数据类型。 + +Map 对象记得键的原始插入顺序。 + +Map 对象具有表示映射大小的属性。 + +| Method | Description | +| :-------- | :----------------------------- | +| new Map() | 创建新的 Map 对象。 | +| set() | 为 Map 对象中的键设置值。 | +| get() | 获取 Map 对象中键的值。 | +| entries() | 返回 Map 对象中键/值对的数组。 | +| keys() | 返回 Map 对象中键的数组。 | +| values() | 返回 Map 对象中值的数组。 | + +## Map() 属性 + +| Property | Description | +| -------- | ------------------------- | +| size | 获取 Map 对象中某键的值。 | + +## 创建 Map 对象 + +能够使用对象作为键是 Map 的一个重要特性。 + +### 实例 + +```js +// 创建对象 +const apples = {name: 'Apples'}; +const bananas = {name: 'Bananas'}; +const oranges = {name: 'Oranges'}; + +// 创建新的 Map +const fruits = new Map(); + +// Add new Elements to the Map +fruits.set(apples, 500); +fruits.set(bananas, 300); +fruits.set(oranges, 200); + +可以将 Array 传递给 new Map() 构造函数: +// 返回 +const apples = {name: 'Apples'}; +const bananas = {name: 'Bananas'}; +const oranges = {name: 'Oranges'}; + +// 创建新的 Map +const fruits = new Map([; + [apples, 500], + [bananas, 300], + [oranges, 200] +]); +``` + +## 获取键的值 + +get() 方法获取 Map 中键的值: + +实例 + +```js +fruits.get(apples); // 返回 500 +``` + +## 其他 Map() 方法 + +| 方法 | 描述 | +| :-------- | :------------------------ | +| clear() | 删除 Map 中的所有元素。 | +| delete() | 删除由键指定的元素。 | +| has() | 如果键存在,则返回 true。 | +| forEach() | 为每个键/值对调用回调。 | + +### Map() 属性 + +| 属性 | 描述 | +| :--- | :------------------ | +| size | 获取 Map 中键的值。 | + +### 实例 + +```js +fruits.size; + +Map.delete() 删除 Map 元素: +fruits.delete(apples); + +Map.clear() 从 Map 中移除所有元素: +fruits.clear(); + +如果 Map 中存在键,则 Map.has() 返回 true: +fruits.has(apples); +``` + +实例 + +```js + // 1.将map转成对象 + // map.set('name','Zoe').set('age',16).set('gender','male') + var map = new Map + map.set('name','Zoe').set('age',16).set('gender','male') + + var obj = Object.fromEntries(map) + console.log(obj); +=============================================================== + //2.将对象转成map + var arr ={ + age:16, + gender:'male', + name:'Zoe' + } + var map1 =Object.entries(arr) + console.log(arr); +========================================================= + map转数组 [] + //const arr = [...map] + // const arr = [] + // for(let e of map.entries()){ + // arr.push(e) + // } + + //数组转map + // const arr = [['name', 'zoe'], ['age', 18]] + // var map = new Map([ + // ['name', 'zoe'], ['age', 18] + // ]) +``` + + + +# 2022-10-28 + +## 预编译 + +1.执行函数之前创建一个AO(activation object)对象:临时存储容器 + +2.找形参和变量声明,值赋予undefined + +3.将形参与实参的值统一 + +4.找函数声明,值赋予函数体 + +```js +1.执行函数之前创建一个AO(activation object)对象:临时存储容器 +AO={} +2.找形参和变量声明,值赋予undefined +3.将形参与实参的值统一 +4.找函数声明,值赋予函数体 +示例: + 1. global = 100; + function fn(){ + console.log(global);//undefined + global = 200; + console.log(global);//200 + var global =300;//局部变量,如果删了,就是 100 ,200 + } + fn(); + var global; + 2. a = 100; + function test(e){ + function e() {} + arguments[0] = 2; + console.log(e);//2 + if(a){ //应该 无法运行if + var b = 12 // 应该 var b 创建了,但没声明 + } + var c; + a =10; + var a; + console.log(b);//12 错误是undefined + f = 123; + console.log(c);//undefined + console.log(f);//123 + } + test(a) +``` +