当前位置:网站首页>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
边栏推荐
- 【Harmony OS】【ARK UI】Date 基本操作
- 肖sir___面试就业课程____app
- DDL操作数据库、表、列
- 基于Streamlit的YOLOv5ToX模型转换工具(适用YOLOv5训练出来的模型转化为任何格式)
- 常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
- Mysql如何建立索引实现语句优化
- 7.Keras开发简介
- 修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
- Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
- Redis缓存雪崩、缓存穿透、缓存击穿
猜你喜欢

Live | StarRocks technology insider: low base dictionary global optimization

汇编题答案

SM30 表维护视图数据保存前 数据校验事件

种草一个让程序员男友编程时,记住一辈子的 IDEA 神仙插件!

C# WPF设备监控软件(经典)-上篇

那些让电子工程师崩溃瞬间,你经历了几个呢?

Assembly answers

Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

工程制图点的投影练习
随机推荐
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
install ambari
Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
CyberArk被评为2022年Gartner特权访问管理魔力象限领导者
How many moments have you experienced the collapse of electronic engineers?
【精讲】利用原生js实现todolist
SM30 表维护视图数据保存前 数据校验事件
Dialog manager in the fourth chapter: the dialog message loop
DC-5靶场下载及渗透实战详细过程(DC靶场系列)
ORACLE中文乱码
Shell条件语句判断
我将GuiLite移植到了STM32F4开发板上
Shell编程的条件语句
软件开发的最大的区别是什么?
C#异步和多线程
MCM箱模型建模方法及大气O3来源解析
中断系统需要解决的问题
工程制图第九章作业
Windows 安装PostgreSQL
7.Keras开发简介