微一案笔试题分享

1 哪些操做会引发内存泄漏,如何发现

一些常见的内存泄露代码

// 意外的全局变量
functuon foo () { bar = 1} //函数里直接对未定义的变量赋值,致使变量存在顶部Window中,内存垃圾没法回收

//闭包变量被引用致使没法被回收
function f() {
    var obj = { a: 2 }
    return obj;
}    
var a =  f()

//被遗忘的定时器

function Test()  {  
    this.obj= {};
    this.index = 1;
    this.timer = null;
    var cache = []; // 内部变量,内存隐患...
    this.timer = window.setInterval(() =>{
        this.index += 1; 
        this.obj = {
            val: '_timerxxxxxbbbbxx_' + this.index,
            junk: [...cache]
        };
        cache.push(this.obj);
    }, 1);  
    console.warn("create Test instance..");
}  
test = new Test(); // JS对象开启定时器不断分配内存


...

参考文章:前端

https://juejin.im/post/5a8e7f...vue

https://github.com/wengjq/Blo...node

如何查看内存占用状况

web

googol控制台 》 performance 面板 > 勾选 Memory
点击左上角的录制按钮。
在页面上进行各类操做,模拟用户的使用状况。
若是内存占用基本平稳,接近水平,就说明不存在内存泄漏。反之,就是内存泄漏了。react

node

console.log(process.memoryUsage()); //nodejquery

2 如下代码输出

typeof Vue
typeof React
typeof jQery

function github Vuegit

object github Reactgithub

function github Jquery web

ps:话说我写了这么久Vue。还历来没操做过typeof vue。。。。面试

3 下面代码输出

class F {
  init () {
        console.log(1)
  }
}
var f = new F()

F.prototype.init = function () {
  console.log(2)
}

f.init() //2

4 如何在数组头部、尾部、中部增长/删除

头部:unshift / shift

中部:splice / concat

尾部: push / pop

参考:https://developer.mozilla.org...segmentfault

5 手写防抖/节流 实现

function throttleAndDebounce(fn, delay, isThrottle) {
  let lastCallTime = 0, timer = null;
  return  (...args) => {
    if (isThrottle) {
      const now = Date.now()
      if (now - lastCallTime < delay) return
      lastCallTime = now
      fn(...args)
    } else {
      if (timer) clearTimeout(timer)
      timer = setTimeout( () => {
        fn(...args)
      }, delay)
    }
  }
}

6 filter/reduce 实现数组去重

var arr = [1,2,1]

arr.filter( (it, idx, arr) => {
  return arr.indexOf(it) === idx
})

// reduce
var reducer = (arr, cur) => {
  if ( arr.length === 0 || arr[arr.length - 1] !== cur) {
    arr.push(cur)
  }
  return arr
}
arr.sort().reduce(reducer, [])

7 原生实现 上传base64 图片

<input id="file" type="file" />
var file = document.getElementById('file').files[0]
var reader = new FileReader()
    reader.onload = function (e) {
      $.post('/upload' , { "base64": e.target.result } , function () {})
    }
    reader.readAsDataURL(file)

8 写成3种前端下载文件方式

参考: https://segmentfault.com/a/11...

ps:这也算!!?浏览器打开。。。心里一度崩溃,真的是为了面试而面试。。

9 手写promise 实现

参考:

https://www.jianshu.com/p/43d...

https://developer.mozilla.org...

10 vue实现数据绑定有什么缺陷?做者为何改用proxy实现

参考:https://zhuanlan.zhihu.com/p/...

后记

有些问题 我没给出答案,只给出一些参考连接,主要是才疏学浅,不能给出一个绝对完美的答案;或者答案的内容量能够再写一篇深刻专研的文章,你们有什么好的意见或者文章错误能够留言补充;欢迎技术交流

ps:一年没面试了第一次作这种笔试题,我表示一个笔都很久没握过的人瑟瑟发抖。。。建议你们不要裸辞。。这个夏天有点冷。。。

若是以为本文对你有所帮助,就star一下吧~大传送之术! 个人博客Github

相关文章
相关标签/搜索