手撸一些面试知识点

1、如何实现深拷贝?

思路:先判断输入的是数组还是对象,根据输入创建对应的新接收类型,判断拷贝完之后每个数值是不是还是对象,如果是继续拷贝!

function deepClone (obj) {
	// 判断是不是Array的实例
	var newObj = obj instanceof Array ? [] : {}
	for (var i in obj) {
      newObj[i] = typeof  obj[i] == 'object' ? deepClone (obj[i]) : obj[i] 
   }
   return newObj
}

(这个好像不能真正实现深拷贝,最简单的深拷贝,可以用JSON.parse()和JSON.stringify())
2、如何实现浅拷贝?

function clone (obj) {
	var newObj = obj instanceof Array ? [] : {}
	for (var i in obj) {
	   newObj [i] = obj[i]
	}
	return newObj
}

3、如何实现图片懒加载?

思路: 图片里面只要有src浏览器就会发送请求,过多的图片src会发送很多请求使页面变得很卡,所以只要出现在浏览视窗的时候才让其显示发送请求,这样能够避免一次性发送太多请求!通过属性,data-xxx的方式,只要图片进入视窗就把其赋值给src属性!当图片距离offsetTop <= window.clientHeight + scrollTop的时候表示已经出现在视窗可以加载图片!

<div id="pice">
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
	<img src=''../假的src' data-src="../真的src"></img>
</div>

function Imgload (el) {
	var ImgArr = document.getElementById(el).getElementByTagName('img')
	var top = window.clientHeight () + window.scrollTop ();
	for (var i =0 ; i<ImgArr.length; i++) {
		if (imgArr[i].offsetTop <= top){
		imgArr[i].src = imgArr[i].getAttribute('data-src')
		}
	}
}

window.onload = Imgload(pice)

4、如何实现sleep效果?

思路:实现一个在设定的时间内过后,再执行的函数,并且是异步执行的

function sleep (time) {
	return new Promise ((resolve) => setTimeout (resolve, time))
}

async function test () {
	const result = await sleep (1000) // 等待执行完promise之后再执行输出
	console.log(111)
	return result
}
test ()

5、如何实现防抖函数?

在一些浏览器的操作中,比如scroll,resize事件函数会不断的执行,高频的触发,所以希望能够在一定的时间内,把多次触发的函数,合并成一次触发,这就是防抖

function debounce (fn, await) {
	var timer = null
	
	return function () {
	 	clearTimeout(timer )
		timer = setTimeout(fn, await)
	}
}

function test () {
	console.log(111)
}

window.scroll(debounce(test, 500))

6、如何实现节流函数?

我们希望即使页面在不断被滚动,但是滚动 handler 也可以以一定的频率被触发(譬如 250ms 触发一次),这类场景,就要用到另一种技巧,称为节流函数(throttling)

function throttl (fn, await, mustRun) {
	var timer;
	var startTime = new Date();
	return function () {
		var content = this
		var args = arguments
		var endTime = new Date()
		clearTimeout(timer )
		if (endTime - startTime >= mustRun) {
			fn.apply(content, args)
			startTime = endTime
		} else {
			timer = setTimeout(fn, await)
		}
	}
}

function test () {
	console.log(111)
}

window.addEventListener('scroll', throttl (test, 500, 1000))

7、如何实现once函数?

function once(fn) {
	var flag = true
	return function () {
		var content = this
		var args = arguments
		if (flag == true) {
			fn.apply(content, args)
		}
		flag = false
	}
	return undefined
}

8、如何用构造函数实现继承

// 父类
function SupFather (name) {
	this.name = name
	this.colors= ['red', 'green', 'yellow'
}
// 父类原型上的一个sayName方法
supFather.prototype.sayName = function (age) {
	console.log(this.name, 'age')
}
// 子类
function Sup(name,age) {
	supFather.call(this, name)
	this.age = age
}
// 重写子类的prototyp,修正子类constructor指向
function inheritPrototype (sonFu, fatherSup) {
	sonFu.prototype = Object.create(fatherSup.prototype)
	sonFu.prototype.constructor = sonFu
}
inheritPrototype (sup, supFather)
// 为子类原型绑定一个sayAge方法
sup.prototype.sayAge = function () {
	console.log(this.age, 'foo')
}
//实例化子类
const instance1 = new sup('Kobe', 29)
instance1.colors.push('black')
console.log(instance1)  // {"name": "Kobe", "colors":["red", "green", "yellow", "black"], "age": 29}

9、如何实现继承(ES6)

// 父类
class SupFather {
	constructor (name) {
		this.name = name
		this.colors = ["red", "yellow", "green"]
    }
    sayName () {
		console.log(this.name, 'age')
	}
}
// 子类
class Sup extends SupFather {
	constructor (name, age) {
		super(name)
		this.age = age
	}
	sayAge () {
		console.log(this.age, 'foo')
	}
}

// 实例化子类
const instance = new Sup("kobe", 33)
const arr  = instance.colors.push("black")
console.log(arr)
instance.sayName()
instance.sayAge()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值