手写js面试题集

  • 实现一个遍历domtree

function domtree(dom,index=0){
	index++;//domtree深度
	if (dom.nodeType === Node.ELEMENT_NODE) {
		console.log(dom.tagName+":"+index)
	}
	let childNodes = dom.childNodes;
	for (let i=0;i<childNodes.length;i++) {
		if(childNodes[i].nodeType === Node.ELEMENT_NODE){
			domtree(childNodes[i],index)
		}
	}
}复制代码
  • 实现一个instanceof

先上一张图

                       

function  Instanceof(a,b) {
	let proto = a.__proto__;
	let prototype = b.prototype;
	while (true){
		if (proto === prototype) { 
			return true
		}
		if (proto == null) {
			return false
		}
		proto = proto.__proto__;
	}
}复制代码
  • 防抖与节流

1.防抖
function debounce(func,wait){
	let timer = null;
	return function(...prm){
		if(timer) clearTimeout(timer);
		timer = setTimeout(()=>{
			func(...prm)
		},wait)
	}
}复制代码

2.节流

function throttle(fn, wait) {
	let prev = new Date();
	return function() { 
	    const args = arguments;
		const now = new Date();
		if (now - prev > wait) {
			fn.apply(this, args);
			prev = new Date();
		}
}复制代码
  • 深拷贝

function deepCopy(obj){
    if(typeof obj == "object"){
        var result = obj.constructor == Array ? [] : {};
        for(let i in obj){
            result[i] =  deepCopy(obj[i]) ;
        }
    }else {
        var result = obj;
    }
    return result;
}复制代码




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值