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