站点工具

用户工具


前端代码题

闭包

  1. 输出什么? 如果想输出0、1、2、3、4、5怎么解决,至少两种方案
    for(var i=0; i<6; i++) {
      setTimeout(() => {
     console.log(i)
      })
    }

异步、Promise输出

  1. 输出什么
    async function async1 (){
    console.log("async1")
    await async2()
    console.log("async1 end")
    }
    console.log("scripts start")
    async1()
    async function async2() {
     await console.log("async2")
    }
    new Promise((resolve,reject)=>{
      console.log("promise1")
      resolve()
    }).then(()=>{
      console.log("promise2")
    })
    setTimeout(()=>{
      console.log("setTimeout")
    })

this、作用域、声明前置、隐式转换

  1. 以下代码输出

    var a = function(){ this.b = 3 }
    var c = new a()
    a.prototype.b = 10
    var b = 7
    a()
    console.log(b)//3
    console.log(c.b)//3
  2. 代码输出什么

    function Foo() {
      getName = function () {
     console.log(1);
      }
    }
    var getName = function () {
      console.log(4);
    };
    function getName() {
      console.log(5);
    }
    Foo.getName = function () {
      console.log(2)
    }
    Foo.prototype.getName = function () {
      console.log(3);
    }
    Foo.getName()     
    getName()  	    
    new Foo.getName()    
  3. 隐式转换
    if ([]) console.log(1);
    if ([].length) console.log(2);
    if ([] == 0) console.log(3);
    if ({} === {}) console.log(4);

算法

  1. 给两个数组[1,2,3],[2,1,3,4],如果A包含于B返回1,B含于A返回2,其他返回0
  2. 无重复的字符串字串
  3. 合并两个有序链表
  4. 最长回文子串
  5. 最长无重复字符子串
  6. 搜索二叉树,找出第k大
  7. 很长的字符串返回出现次数最多的字母以及相应次数
  8. 多叉树的最大深度

手写

  1. 手写一个Promise.all (3)
  2. 手写Promise,包含简单的逻辑即可
  3. 用reduce实现map
  4. 手写一个repeat函数,要求每隔3秒alert一次helloWorld,总共输出4次
    function repeat(func, times, wait){
      ...
    }
    var rp = repeat(alert, 4, 3)
    rp('helloWorld')
  5. 字符串转化为数字
    // 'A' => 1
    // 'B' => 2
    // 'AA' => 27
  6. 手写观察者(3), 带once
  7. 用call/apply实现bind
  8. 实现大数相加
  9. 手写防抖节流
  10. 实现并发控制
  11. 写一个instanceof的实现
若愚 · 2022/01/24 15:28 · 前端专刷_代码题.1643009330.txt.gz