blog's Introduction
blog's People
blog's Issues
call/apply/bind用法
-
总结
call
apply
bind
的作用都是将this
绑定到指定的对象上- 三者的第一个参数都是
this
指向的对象 - 三者都可以给定参数传递
call
给定参数需要将参数全部列出,apply
给定参数数组bind
返回函数可稍后执行,call apply
都是立即执行
-
call
Function.prototype.call
- 语法:
func.call(thisArg,arg1,arg2...)
thisArg
是undefined
或者null
时函数的this
值会替换成全局对象
let person = { name: 'xm', age: 18 } function say(job){ console.log(`${this.name} : ${this.age} ${job}`) } // call 将 say 方法用于person对象中 // say 内部的this指向person这个对象 say.call(person,'it')
-
apply
Function.prototyp.apply
- 语法:
func.apply(thisArg,[args])
apply
与call
的区别:call
的参数是列表,apply
参数是一个数组
let person = { name: 'x', age: 18 } function say(job){ console.log(`${this.name} : ${this.age} ${job}`) } // apply 与 call 区别 // call 的参数是列表,apply 参数是一个数组 say.apply(person,['it'])
- 自定义apply
Function.prototype._apply = function(context){ let context = context || window; let fn = Symbol(); context[fn] = this; //参数数组 let args = arguments[1]; if(!args){ context[fn](); } else{ context[fn](...args); } //从上下文中删除函数的引用 delete context.fn; ]}
-
bind
- 语法:
func.bind(thisArg,arg1,arg2...)
bind()
方法创建一个新的函数(成为绑定函数),是原函数的一个拷贝,bind
不会像call
或apply
那样立即执行bind()
返回一个绑定函数可稍后执行,call
apply
是立即调用
function Person(name){ this.name = name; this.say = function(){ setTimeout(function(){ console.log("hello " + this.name); }.bind(this),1000) } } var person = new Person("axuebin"); person.say(); //hello axuebin
- 语法:
发布订阅模式 [ pub-sub ]
class EventBus {
constructor(){
this.eventList = new Map()
}
$emit(ename, params, once = false){
const cbList = this.eventList.get(ename)
cbList && cbList instanceof Array && cbList.forEach(
cb => typeof cb === 'function' && cb(params)
);
once && this.$off(ename)
}
$on(ename, cb){
if(!ename || !cb) return;
let cbList = this.eventList.get(ename)
!cbList && (cbList = [])
cbList.push(cb)
this.eventList.set(ename,cbList)
return cb
}
$off(ename,cb){
// 默认清空所有事件
!ename && (this.eventList = new Map())
// 不传cb, 则清除ename下所有事件
!cb && (this.eventList.get(ename) = [])
const cbList = this.eventList.get(ename)?.filter(callback=>callback!=cb)
this.eventList.set(ename,cbList)
}
}
防抖函数(Debounce)和节流函数(throttle)
// 防抖:函数在某一时间内多次调用只有一次执行
// 第一次执行会设置一个定时器并缓存,
// 之后每次执行就会清除之前的定时器并重新设置
// 定时器计时结束后触发函数执行
function debounce(cb, time){
// 定时器
let timer = null;
return (...args)=>{
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(()=>{
cb(...args)
}, time)
}
}
// 节流函数,在一定时间内只允许一次函数执行
function throttle(cb,time){
let flag = false
return (...args)=>{
if(flag) return
flag = true
setTimeout(()=>{
cb(...args)
flag = false
},time)
}
}
window.onmouseover = debounce(e=>{
console.log(e);
},500)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.