• 前端面试准备——day03


    1 使下面的语句使用展开运算而不导致类型错误

     var obj={x:1,y:2,z:3}
       obj[Symbol.iterator]=function(){
          return {
            next:function(){
              let objArr=Reflect.ownKeys(obj)  //symbol作为对象键名时,使用object.keys是无法遍历出来的
              if(this.index<objArr.length-1){
                let key=objArr[this.index]
                this.index++
                return {
                  value:obj[key]
                }
              }else{
                return {
                  done: true
                }
              }
    
            },
            index:0
          }
       }
       
        console.log([...obj]);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    对象合并

    {...obj1,...obj2}
    
    • 1

    2 圣杯布局

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        .container{
          margin: 0 120px;
        }
        .main{
          float: left;
          width: 100%;
          height: 300px;
          background-color: pink;
        }
        .left{
          position: relative;
          left: -100px;
          float: left;
          width:100px;
          height: 300px;
          background-color: red;
          margin-left: -100%;
        }
        .right{
          position: relative;
          right: -200px;
          float: right;
          width: 200px;
          height: 300px;
          background-color: blue;
          margin-left: -100%;
        }
      </style>
    </head>
    <body>
      <div class="container">
        <div class="main"></div>
        <div class="left"></div>
        <div class="right"></div>
      </div>
      
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3 CSRF攻击

    跨站请求伪造:通过恶意引导用户一次点击劫持cookie进行攻击

    类型

    • GET类型:比如在img标签中存在一个请求,当用户打开包含这个img标签的网页时,请求会自动发送
    • POST类型:比如构建一个隐藏表单,当用户进入界面时,自动提交表单
    • 链接类型:比如利用a标签的href属性,诱导用户进行点击

    防御策略

    (1)同源策略(禁止外域对我们发起请求)

    服务器根据http请求头中的Origin信息和referer信息判断请求是否为允许访问的站点,当Origin信息和referer信息都不存在时,直接阻止

    (2)CSRF Token
    服务器向用户返回一个随机token,当网站再次发起请求时,在请求参数中加入返回的token,服务器对比保存在session中token是否匹配,缺点是每次请求都需要带上token

    (3)双重Cookie验证

    (4)在设置Cookie的时候设置Samesite
    限制cookie不能被第三方使用

    4 找出字符串出现最多的次数

    let str='aaaaaabbbbbbbbbbbbcccccccccc'
    
        function most(str){
          let map=new Map()
          for(let v of str){
            if(map.has(v)){
              map.set(v,map.get(v)+1)
            }else{
              map.set(v,1)
            }
          }
          let temp=0
          for(let v of map.values()){
            temp=Math.max(v,temp)
          }
          return temp
        }
        console.log(most(str));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5 获取20-50的随机数

    function random(min,max){
          return Math.random()*(max-min)+min
        }
        console.log(random(20,50));
    
    • 1
    • 2
    • 3
    • 4

    6 事件委托

    <ul>
    	<li></li>
    	<li></li>
    	<li></li>
    </ul>
    
    var ul=document.getElementById('ul')
    
    
    ul.onclick=function(event){
    	event=event|| window.event
    	var target=event.target
    	if(target.nodeName==='LI'){
    		alert(target.innerHTML)	
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    7 写一个函数判断数据类型

    function check(value){
          //数组和对象都为对象
          if(typeof value === 'object'){
            if(Object.prototype.toString.call(value)==='[object Object]'){
              return 'object'
            }else{
              return 'array'
            }
          }else{
            return typeof value
          }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    8 计算sum(1,2,3)和sum(1)(2)(3)

    //将参数存放在数组中
        function sum(){
          let args=[...arguments]
          const add=()=>{
            args.push(...arguments)
            return add
          }
          //调用toString方法获取结果
          add.toString=()=>args.reduce((a,b)=>a+b,0)
          return add
        }
        console.log(sum(1,2)(3).toString());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    9 登录弹窗

    var login=(function(){
          var div=null
          return function(){
            if(!div){
              div=document.createElement('div')
              div.innerHTML='登录成功'
              div.style.display='none'
              document.body.append(div)
            }
            return div
          }
        })()
    
    
        document.getElementById('login').onclick=()=>{
          var myLogin=login()
          myLogin.style.display='block'
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    10 reduce的应用

    求和、计算每个元素的出现次数、去重、将多维数组转化为一维数组

    11 cookies、sessionStorage和localStorage的区别

    1、cookie是网站为了标识用户身份而存储在用户本地终端上的数据,每次发送http请求时,会自动携带cookie,而sessionStorage、localStorage不会自动把数据发给服务器,仅在本地保存
    2、cookie数据最大为4k,而其他两个为5M
    3、cookie在设置的过期时间之前一直有效,sessionStorage仅在当前窗口有效,localStorage永久有效

    12 隐式转换

    1+'undefined' //NaN
    1+'str'		//'1str'
    1+true		//2
    1+null		//1+0=1
    
    '2'>10		//false
    '2'>'10'	//当关系运算符两边都是数字,按照字符串对应unicode编码转换
    
    undefined==undefined  //true
    undefined==null //true
    null==null //true
    NaN==NaN //false
    
    
    //逻辑非运算符 将其他数据类型用Boolean()转换
    // 0 NaN undefined null '' false 转换为boolean值为false,其他都为true
    []==0	//true	Number([].valueof().toString())==0
    ![]==0	//true  !Boolean([])
    
    []==![]	//true	Number([].valueof().toString())==0 ![]==false
    []==[]	//false
    
    {}==!{}	//false	{}.valueOf().toString()='[object Object]' 
    {}=={}	//false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    小编推荐几款好用的MySQL开源客户端,建议收藏哦
    动态代理看这个就够了
    Java_多态/动态绑定机制/多态数组/多态参数
    JS 找到字符串中所有的数字部分
    win11系统前端IIS部署发布网站步骤
    KY91 String Matching
    数据采集之:巧用布隆过滤器提取数据摘要
    漏刻有时数据可视化Echarts组件开发(40)pictorialBar象形柱图
    通过 Azure OpenAI 服务使用 GPT-35-Turbo and GPT-4(win版)
    Windbg 命令 (一)
  • 原文地址:https://blog.csdn.net/weixin_44208404/article/details/125586573