当前位置:网站首页>JS底层手写
JS底层手写
2022-08-03 04:27:00 【weixin_46051260】
instanceOf
function myInstanceof(left, right) {
//基本数据类型直接返回false
if(typeof left !== 'object' || left === null) return false;
//getProtypeOf是Object对象自带的一个方法,能够拿到参数的原型对象
let proto = Object.getPrototypeOf(left);
while(true) {
//查找到尽头,还没找到
if(proto == null) return false;
//找到相同的原型对象
if(proto == right.prototype) return true;
proto = Object.getPrototypeOf(proto);
} }
防抖
var btn=document.querySelector('button')
var ipt=document.querySelector('input')
btn.addEventListener('click',debounce(getValue,2000))
function getValue(){
var val=ipt.value
console.log(val);
}
function debounce(fn,time){
let t=null
return function(){
if(t){
clearTimeout(t)
}
var firstClick=!t
if(firstClick){
fn.apply(this,arguments)
}
t=setTimeout(() => {
t=null
}, time);
}
}
节流
var btn=document.querySelector('button')
var ipt=document.querySelector('input')
btn.addEventListener('click',throttle(getValue,2000))
function getValue(){
var val=ipt.value
console.log(val);
}
function throttle(fn,time){
var begin=0
return function(){
var date=new Date().getTime()
if(date-begin>time){
fn.apply(this,arguments)
begin=date
}
}
}
call
Function.prototype.myCall = function (obj) {
var obj = obj || window
obj.fn = this//指向person
var args = [...arguments].slice(1)
var result = obj.fn(...args)
// 删除 fn
delete obj.fn
return result
}
function person(a,b,c){
return {
name:this.name,
a:a,b:b,c:c
}
}
var obj={
name:'jack'
}
var bili=person.myCall(obj,1,2,3)
console.log(bili);
apply
Function.prototype.myApply = function (context,arr) {
var context = context || window
context.fn = this
// 需要判断是否存储第二个参数
// 如果存在,就将第二个参数展开
if (arr) {
result= context.fn(...arr)
} else {
result = context.fn()
}
delete context.fn
return result
}
function person(a, b, c) {
return {
name: this.name,
a: a, b: b, c: c
}
}
var obj = {
name: 'jack'
}
console.log(person.myApply(obj,[1,2,3]));
bind
边栏推荐
- 普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
- 中断系统需要解决的问题
- Redis-Redisson介绍和用途
- Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
- OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
- t conditional judgment statement and if loop
- 工程制图第九章作业
- 荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
- 刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
- 社交电商:流量红利已尽,裂变营销是最低成本的获客之道
猜你喜欢
随机推荐
v-text指令:设置标签内容
让环境自己说话,论环境自描述的重要性
DC-4靶场搭建及渗透实战详细过程(DC靶场系列)
TCP 和UDP 的详细介绍
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
t条件判断语句与if循环
肖sir___面试就业课程____性能测试
工程水文学试题库
IO进程线程->线程->day5
解析,强势供应商的管理方法
LeetCode算法日记:面试题 03.04. 化栈为队
CobalStrike(CS)基础超级详细版
那些让电子工程师崩溃瞬间,你经历了几个呢?
v-on指令:为元素绑定事件
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
寄存器(内存访问)
基于WPF重复造轮子,写一款数据库文档管理工具(一)
测试人员的价值体现在哪里
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
Jmeter 模拟多用户登录的两种方法