Call,Apply,Bind的使用与区别,如何实现一个bind?前端
相同点:web
不一样点:安全
基本使用:bash
Array.prototype.slice.call(obj,0,1,2)
Array.prototype.slice.apply(obj,[0,1,2])
Array.prototype.slice.bind(obj)(0,1,2)
复制代码
从上面的例子能够看出来call,apply 使用上几乎保持一致,而bind其实是返回了一个函数app
简易bind实现模块化
Function.prototype.bind = function(context){
const _this = this
return function() {
_this.apply(context, Array.prototype.slice.call(arguments))
}
}
复制代码
上面的bind只实现了方法的做用域绑定,参数已经固定,若是想要动态的参数咱们得改写一下函数
Function.prototype.bind = function(context){
const _this = this
const argus = Array.prototype.slice.apply(arguments,[1])
return function() {
_this.apply(context, argus.concat(Array.prototype.slice.call(arguments)))
}
}
复制代码
JS每日一题: 说说你对前端模块化的理解
JS每日一题: web安全攻击手段有哪些?以及如何防范web安全
JS每日一题能够当作是一个语音答题社区
天天利用碎片时间采用60秒内的语音形式来完成当天的考题
群主在第二天0点推送当天的参考答案post