当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
easyswoole的mysqli 事务怎么写
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
肖sir__简历
MySQL 删除表数据,重置自增 id 为 0 的两个方式
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
肖sir___面试就业课程____app
mysql bool盲注
rosbag工具plotjuggler无法打开rosbag的问题
EssilorLuxottica借助Boomi的智能集成平台实现订单处理的现代化
【Harmony OS】【ARK UI】Date 基本操作
Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
6.神经网络剖析
我将GuiLite移植到了STM32F4开发板上
肖sir__面试就业课___数据库
Assembly answers
Redis缓存雪崩、缓存穿透、缓存击穿
DDL操作数据库、表、列
Jmeter 模拟多用户登录的两种方法









