• 前端面试(手写题)


    必背面试题web前端面试题(必背面试题)_Z_Xshan的博客-CSDN博客_web前端面试题

    面试官:手写防抖与节流

    1. //防抖
    2. function debounce(func,wait=5000){
    3. let timer=0;
    4. return function(...args){
    5. if (timer) clearTimeout(timer)
    6. timer=setTimeout(()=>{
    7. console.log(2);
    8. func.apply(this,args)
    9. },wait)
    10. }
    11. }
    12. //节流
    13. function throttle(func,delay=2000){
    14. var timer=1;
    15. return function(...args){
    16. if(timer){
    17. timer=setTimeout(()=>{
    18. func.apply(this,args)
    19. timer=0
    20. },delay)
    21. }
    22. }
    23. }

    面试题:实现instanceof

    1. function _instanceof(example,classFunc){
    2. if (typeof example!=='object' || example==null) return false
    3. let proto=Object.getPrototypeOf(example);
    4. while(true){
    5. if (proto==classFunc.prototype) return true;
    6. proto=Object.getPrototypeOf(proto)
    7. }
    8. }
    9. console.log(_instanceof([], Array)); //true

    面试题:实现new方法

    1. function myNew(fn, ...args){
    2. // 基于原型链 创建一个新对象
    3. let newObj=Object.create(fn.prototype);
    4. // 添加属性到新对象上 并获取obj函数的结果
    5. let res=fn.apply(newObj,args)
    6. // 如果执行结构有返回值并是一个对象,返回执行结果,否则,返回新创建的对象
    7. return typeof res==='object'?res:newObj;
    8. }
    9. // 用法
    10. function Person(name,age){
    11. this.name=name;
    12. this.age=age;
    13. }
    14. Person.prototype.say=function(){
    15. console.log(this.age);
    16. }
    17. let p1=myNew(Person,'poety',18);
    18. console.log(p1.name);
    19. console.log(p1);
    20. p1.say

    面试题:实现深拷贝

    1. let obj = {
    2. val: [12, 4, 'pp'],
    3. name: 'name',
    4. age: 12,
    5. obj: {
    6. name: 'n',
    7. age: 3
    8. },
    9. }
    10. function deepClone(source) {
    11. let tarGetobj = source.constructor === Array ? [] : {};
    12. for (let keys in source) { //如果是数组循环索引 对象循环键名
    13. if (source[keys] && source[keys] instanceof Object) {
    14. tarGetobj[keys] = deepClone(source[keys])
    15. } else {
    16. tarGetobj[keys] = source[keys]
    17. }
    18. }
    19. return tarGetobj
    20. }
    21. let new obj = deepClone(obj);

  • 相关阅读:
    ES相关面试问题整理
    API测试基础之http协议
    【Vant Weapp】van-uploader 文件上传
    WPF中非递归(无后台代码)动态实现TreeView
    一个包搞定中文数据集: datasetstore
    设计模式--kotlin&java
    如果保障服务器的安全性
    【数据结构】| 并查集及其优化实现
    Linux系统日志采集
    leetcode 355 设计推特
  • 原文地址:https://blog.csdn.net/Z_Gleng/article/details/128155852