当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
DDL操作数据库、表、列
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
DC-6靶场下载及渗透实战详细过程(DC靶场系列)
unity2D横板游戏教程6-敌人AI以及受击动画
js中的闭包
Live | StarRocks technology insider: low base dictionary global optimization
13.机器学习基础:数据预处理与特征工程
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
MySQL 出现 The table is full 的解决方法
OpenFOAM提取等职面并计算面积
安装ambari
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
DC-5靶场下载及渗透实战详细过程(DC靶场系列)
Shell条件语句判断
"Obs" start pushing flow failure: the Output. The StartStreamFailed call process
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
自组织是管理者和成员的双向奔赴
第3周 用1层隐藏层的神经网络分类二维数据
Test drive: project management module - curd development project
传统企业如何转型社交电商,泰山众筹的玩法有哪些?