当前位置:网站首页>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
边栏推荐
- LeetCode算法日记:面试题 03.04. 化栈为队
- easyswoole的mysqli 事务怎么写
- Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
- 工程制图第九章作业
- excerpt from compilation book
- Assembly answers
- 种草一个让程序员男友编程时,记住一辈子的 IDEA 神仙插件!
- 关于#sql#的问题,如何解决?
- 肖sir___面试就业课程____性能测试
- online test paper concept
猜你喜欢
随机推荐
接口测试如何准备测试数据
redis键值出现 xacxedx00x05tx00&的解决方法
DFS对剪枝的补充
3.张量运算
flink sql任务变更,在sql里面增加几个字段后,从以前保存的savepoint恢复启动出错。
OpenFOAM extracts equivalency and calculates area
测试人员的价值体现在哪里
浏览器监听标签页关闭
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
Redis连接不上的报错解决方案汇总
肖sir__自动化面试题
富瑞宣布战略交易,以简化运营,持续专注于打造领先的独立全服务型全球投行公司
lc marathon 8.2
TCP 和UDP 的详细介绍
【Harmony OS】【FAQ】鸿蒙问题合集1
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
8.电影评论分类:二分类问题
自组织是管理者和成员的双向奔赴
工程制图-齿轮
WinForm的控件二次开发









