基礎概念
apply方法
bind方法
模擬實作

Function.prototype._bind = function(target) {
// 保留呼叫_bind方法的物件
let _this = this;
// 接收保存傳入_bind方法中的引數,等價于arguments.slice(1),除了第一個引數其余全視作傳入引數
let args = [].slice.call(arguments, 1)
return function() {
return _this.apply(target, args)
}
}
let obj = {
name: '測驗員小陳'
}
// 測驗函式
function test(args) {
console.log('this:', this);
console.log('我的名字:', this.name);
console.log('我接收的引數:', args);
}
console.log(test._bind(obj, "I am args")); // output: [Function]
test._bind(obj, "I am args")()
/* 執行結果
* this: { name: '測驗員小陳' }
* 我的名字: 測驗員小陳
* 我接收的引數: I am args
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/148802.html
標籤:JavaScript
上一篇:uniapp簡易直播
