当前位置:网站首页>JS bottom handwriting
JS bottom handwriting
2022-08-03 04:31: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
边栏推荐
猜你喜欢
随机推荐
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
LeetCode算法日记:面试题 03.04. 化栈为队
在竞争白热化的电商行业,链动2+1为什么还有企业在用
ORACLE中文乱码
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
寄存器(内存访问)
私域流量引流方法?分享购火爆的商业模式,你值得拥有
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
MySql 创建索引
计组错题集
【翻译】开发与生产中的Kubernetes修复成本对比
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
【Harmony OS】【FAQ】鸿蒙问题合集1
8.电影评论分类:二分类问题
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
C#异步和多线程
接口测试如何准备测试数据
WinForm的控件二次开发
2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫









