前端常见笔试大题解法

1 手写bind call apply
解 这三个都是js自带的方法,三兄弟的作用都是在函数中改变this的指向
call

 Function.prototype.call = function(context){
        const cxt = context || window;
        cxt.func = this;
        const args = Array.from(arguments).slice(1);
        const res = arguments.length > 1 ? cxt.func(...args) : cxt.func();
        delete cxt.func;
        return res;
    }

apply

  Function.prototype.apply = function(context){
        const cxt = context || window;
        cxt.func = this;
        const res = arguments[1] > 1 ? cxt.func(...arguments[1]) : cxt.func();
        delete cxt.func;
        return res;
    }

bind

 Function.prototype.bind = function(context){
        const cxt = JSON.parse(JSON.stringify(context)) || window;
        cxt.func = this;
        const args = Array.from(arguments).slice(1);
        return function(){
          const allArgs = args.concat(Array.from(arguments));
          return allArgs.length > 0 ? cxt.func(...allArgs) : cxt.func();
        }
    }

2 手写节流 防抖
不清楚节流和防抖的可以去看我前面的博文
节流

 function throttle(fn,delay) {
        let time = null;
        return function (){
            if(!time){
                time = setTimeout(() =>{
                    time = null;
                    fn.apply(this, arguments);
                },delay)
            }
        }
    }

防抖

 function debounce(fn) {
      let timeout = null; // 创建一个标记用来存放定时器的返回值
      return function () {
        clearTimeout(timeout); // 每当用户输入的时候把前一个 setTimeout clear 掉
        timeout = setTimeout(() => { // 然后又创建一个新的 setTimeout, 这样就能保证输入字符后的 interval 间隔内如果还有字符输入的话,就不会执行 fn 函数
          fn.apply(this, arguments);
        }, 500);
      };
    }

3 手写ajax
原生请求写法

var data  = new FormData();
data.append("name",name);
data.append("gender",gender);
data.append("age",age);
 xhr.onreadystatechange = function(){
	if(xhr.readyState == 4){
		if(JSON.parse(xhr.responseText)["code"] == 200){
		    alert("返回成功");
        }else{
             alert(JSON.parse(xhr.responseText)["msg"]);
         }
     }
}
xhr.send(data);

4 手写深拷贝 不清楚深拷贝和浅拷贝的可以看我之前的博文

function ArrayCopy(obj = {}){
        if(typeof obj !== "object" || obj === null){
            return obj;
        }
        var agent;
        if(obj instanceof Array){
            agent = [];
        }else{
            agent = {};
        }
        for(let key in obj){
            agent[key] = ArrayCopy(obj[key]);
        }
        return agent
    }

5 手写对象比较

function isObject (data) {
      return typeof data === 'object' && data !== null
    }
    function isEqual (obj1, obj2) {
      if (!isObject(obj1) || !isObject(obj2)) {
        return obj1 === obj2
      }
      if (obj1 === obj2) {
        return true
      }
      const obj1Keys = Object.keys(obj1)
      const obj2Keys = Object.keys(obj2)
      if (obj1Keys.length !== obj2Keys.length) {
        return false
      }
      for (let key in obj1) {
        const res = isEqual(obj1[key], obj2[key])
        if (!res) {
          return false
        }
      }
      return true
    }
    isEqual(obj1, obj2)

6 画出原型链图
在这里插入图片描述
7 手写trim考虑兼容性

if(!String.prototype.trim){
    String.prototype.trim = function(){
      return this.replace(/^\s+/,'').replace(/\s+$/,'')
    }
}

8 手写清除浮动clearfix

clearfix:after
{
	content:",";
	height:0;
	display:block;
	visibility:hidden;
	clear:both;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值