当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
MySQL 入门:Case 语句很好用
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
9.新闻分类:多分类问题
我将GuiLite移植到了STM32F4开发板上
v-text指令:设置标签内容
RequestContextHolder
Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
打破传统电商格局,新型社交电商到底有什么优点?
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
私域流量引流方法?分享购火爆的商业模式,你值得拥有
测开:项目管理模块-项目curd开发
WebSocket的实际应用
MySQL 删除表数据,重置自增 id 为 0 的两个方式
直播|StarRocks 技术内幕 :低基数全局字典优化
在竞争白热化的电商行业,链动2+1为什么还有企业在用
移动流量的爆发式增长,社交电商如何选择商业模式
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
redis键值出现 xacxedx00x05tx00&的解决方法
数据库性能系列之索引(中)