ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
**1.TS 类型有哪些?** ![](https://img.kancloud.cn/9d/ce/9dce5c694a99fd23a9e2a7fd12a723e2_355x199.png) **2. 什么是深拷贝?** 「深拷贝」就是在拷贝数据的时候,将数据的所有**引用结构**都拷贝一份。简单的说就是,在内存中存在两个数据结构完全相同又相互独立的数据,将引用型类型进行复制,而不是只复制其引用关系。 分析下怎么做「深拷贝」: 1. 首先假设深拷贝这个方法已经完成,为 deepClone 2. 要拷贝一个数据,我们肯定要去遍历它的属性,如果这个对象的属性仍是对象,继续使用这个方法,如此往复 ``` function deepClone(o1, o2) { for (let k in o2) { if (typeof o2[k] === 'object') { o1[k] = {}; deepClone(o1[k], o2[k]); } else { o1[k] = o2[k]; } } } ``` 递归容易造成爆栈,尾部调用可以解决递归的这个问题,Chrome 的 V8 引擎做了尾部调用优化,我们在写代码的时候也要注意尾部调用写法。递归的爆栈问题可以通过将递归改写成枚举的方式来解决,就是通过for或者while来代替递归。 ## 3. 克隆数组的方法 (一)slice方法 ``` let arr = [1,2,3,4] let arr1= arr.slice() //或者是   let arr1= arr.slice(0) arr[0] = 6 console.log(arr)  // [6, 2, 3, 4] console.log(arr1) ``// [1, 2, 3, 4] ``` (二)自己封装一个myClone函数 ~~~ Array.prototype.myClone = function(){ let newArr=[]; for(let i=0;i<this.length;i++) { newArr.push(this[i]); } return newArr; } let arr = ['aaa','bbb','ccc','wwwww','ddd'] let arr2 = arr.myClone() console.log(arr) //["aaa", "bbb", "ccc", "wwwww", "ddd"] console.log(arr2) //["aaa", "bbb", "ccc", "wwwww", "ddd"] console.log( arr2 === arr ) //false ~~~ (三)展开运算符 [...arr] ~~~ const arr1 = [1, 2]; const arr2 = [...a1]; arr1[0] = 6 console.log(arr1) // [6, 2] console.log(arr2) // [1, 2] ~~~ (四)concat方法 ~~~ var arr1 = [1,2,3] var arr2 = arr1.concat() //或者是 var arr2 = arr1.concat([]) arr1[0] = 6 console.log(arr1) //[6,1,2] console.log(arr2) //[1,2,3] ~~~ (五)Object.assign() ~~~ let arr = [1,2,3,4]; let arr1 = []; Object.assign(arr1,arr); arr[0] = 6; console.log(arr); // [6, 2, 3, 4] console.log(arr1); // [1, 2, 3, 4]    ~~~ (六) ~~~tsx const deepClone(obj)=>JSON.parse(JSON.stringify(obj)) ~~~ ## 数组主要有哪些方法?哪些可以改变原数组?哪些不能改变原数组? ![](https://img.kancloud.cn/66/97/6697660f7988ff87d9650e5f7346eeb8_1575x483.png) ![](https://img.kancloud.cn/59/a4/59a49c8760e1d8410cf565eab697bb34_1575x550.png) ![](https://img.kancloud.cn/65/c8/65c87aaaf7ba7262b4c38e3dedf6b0f4_1576x500.png) ## Session Cookie localStorage 安全性: Session 比 Cookie 安全,Session 是存储在服务器端的,Cookie 是存储在客户端的。 存取值的类型不同:Cookie 只支持存字符串数据,想要设置其他类型的数据,需要将其转换成字符串,Session 可以存任意数据类型。 有效期不同: Cookie 可设置为长时间保持,比如我们经常使用的默认登录功能,Session 一般失效时间较短,客户端关闭(默认情况下)或者 Session 超时都会失效。 存储大小不同: 单个 Cookie 保存的数据不能超过 4K,Session 可存储数据远高于 Cookie,但是当访问量过多,会占用过多的服务器资源。 ![](https://img.kancloud.cn/a2/30/a230a881c36c67b1d7868a4b0b110441_838x510.png) ~~~ var a = 0; var b = a; b++; alert(a); //=>"0" var o = {}; o.a = 0; var b = o; b.a = 10; alert(o.a); //=>"10" ~~~ ![](https://img.kancloud.cn/b8/1b/b81b3f3b8f7cf7d6e8e6c0a54963803b_594x530.png) ~~~ let x = [1, 2, 3]; let y = x; let z = [4, 5, 6]; y[0] = 10; y = z; z[1] = 20; x[2] = z = 30; console.log(x, y, z); ~~~ ![](https://img.kancloud.cn/ff/61/ff616cdf39a1f30d601d90caa0e39d25_590x333.png) ![](https://img.kancloud.cn/5f/66/5f663f975edaec5e7f56de3afde8017c_883x515.png) ## setState何时同步何时异步? **由React控制的事件处理程序,以及生命周期函数调用setState不会同步更新state** 。 **React控制之外的事件中调用setState是同步更新的。比如原生js绑定的事件,setTimeout/setInterval等**。 大部分开发中用到的都是React封装的事件,比如onChange、onClick、onTouchMove等,这些事件处理程序中的setState都是异步处理的。 ## React是怎样控制异步和同步的呢? 在 React 的 setState 函数实现中,会根据一个变量 isBatchingUpdates 判断是直接更新 this.state 还是放到队列中延时更新,而 isBatchingUpdates 默认是 false,表示 setState 会同步更新 this.state;但是,有一个函数 batchedUpdates,该函数会把 isBatchingUpdates 修改为 true,而当 React 在调用事件处理函数之前就会先调用这个 batchedUpdates将isBatchingUpdates修改为true,这样由 React 控制的事件处理过程 setState 不会同步更新 this.state。 ## react hook使用规则 1: 只在最顶层使用 Hook** 不要在循环,条件或嵌套函数中调用 Hook, 2:不要在普通的 JavaScript 函数中调用 Hook。但可以: * ✅ 在 React 的函数组件中调用 Hook * ✅ 在自定义 Hook 中调用其他 Hook css居中 ## (1) text-align: center; ``` div.center{ text-align: center; background: hsl(0,100%,97%); } div.center img{ width: 33%; height: auto; } ``` html ``` <div class="center"> <img src="img.png"> </div> ``` ## (2) margin: 0 auto; ``` <style> div.center{ background: hsl(60,100%,97%); } div.center img{ display: block; width: 33%; height: auto; margin: 0 auto; } </style> <div class="center"> <img src="img.png"> </div> ``` ## (3) table-cell ``` <style> .center-aligned{ display: table; background: hsl(120,100%,97%); width: 100%; } .center-core{ display: table-cell; text-align: center; vertical-align: middle; } .center-core img{ width: 33%; height: auto; } </style> <div class="center-aligned"> <div class="center-core"> <img src="img.png"> </div> </div> ``` ## (4) position: absolute; ``` <style> .absolute-aligned{ position: relative; min-height: 500px; background: hsl(200,100%,97%); } .absolute-aligned img{ width: 50%; min-width: 200px; height: auto; overflow: auto; margin: auto; position: absolute; top:0; left: 0; bottom: 0; right: 0; } </style> </head> <body> <!--水平垂直居中--> <div class="absolute-aligned"> <img src="img.png"> </div> ``` ### (5) translate ``` <style> .center{ background: hsl(180,100%,97%); position: relative; min-height: 500px; } .center img{ position: absolute; top:50%; left: 50%; transform: translate(-50%,-50%); width: 30%; height: auto; } </style> </head> <body> <!--水平垂直居中--> <div class="center"> <img src="img.png"> </div> ``` ### (6) flex ``` <style> .center{ background: hsl(240,100%,97%); display: flex; justify-content: center; align-items: center; } .center img{ width: 30%; height: auto; } </style> <!--水平居中--> <div class="center"> <img src="img.png"> </div> ``` ### (7) calc ``` <style> .center{ background: hsl(300,100%,97%); min-height: 600px; position: relative; } .center img{ width: 40%; height: auto; position: absolute; top:calc(50% - 20%); left: calc(50% - 20%); } </style> <!--水平居中--> <div class="center"> <img src="img.png"> </div> ``` ### (8) margin left 一半 ``` <style> div{ position: absolute; width: 500px; height: 300px; margin: auto; top:50%; left: 50%; margin:-150px 0 0 -250px; background-color: pink; } </style> </head> <body> <div>center</div> ``` ## HTTP 状态码 HTTP 状态码,有`2xx``3xx``4xx``5xx`这几种,比较常用的有以下几种: * `200`正常 * `3xx` * `301`永久重定向。如`http://xxx.com`这个 GET 请求(最后没有`/`),就会被`301`到`http://xxx.com/`(最后是`/`) * `302`临时重定向。临时的,不是永久的 * `304`资源找到但是不符合请求条件,不会返回任何主体。如发送 GET 请求时,head 中有`If-Modified-Since: xxx`(要求返回更新时间是`xxx`时间之后的资源),如果此时服务器 端资源未更新,则会返回`304`,即不符合要求 * `404`找不到资源 * `5xx`服务器端出错了