0%

手写call

ES5实现

// 生成唯一值
function mySymbol(obj) {
var unique = (Math.random() + Date.now()).toString(32).slice(0, 16);
if (obj[unique]) {
return mySymbol(obj);
} else {
return unique;
}
}

// 绑定自己的call方法到Function原型
Function.prototype.myCall = function (target) {

if (typeof this !== 'function') throw Error(this + 'is not a function');

target = target || window;

var args = [];

for (var i = 1; i < arguments.length; i++) {
args.push('arguments[' + i + ']');
}
// 生成唯一值
var unique = mySymbol(target);
// 绑定this
target[unique] = this;
// 执行
eval('target[unique](' + args + ')');
// 用完就抛弃
delete target[unique];

}

ES6实现

// 绑定自己的call方法到Function原型
Function.prototype.myCall = function (target = window) {

if (typeof this !== 'function') throw Error(this + 'is not a function');

const args = [...arguments].slice(1);
// 生成唯一值
const unique = Symbol('fn');
// 绑定this
target[unique] = this;
// 执行
target[unique](...args);
// 用完就抛弃
delete target[unique];

}

调用

function person(a, b) {
console.log(a, b, this.name);
}

var obj = {
name: '靓仔'
};

person.myCall(obj, '我', '是');

小提示

生成唯一值的目的是用来作为 targetkeykey 在对象中是不能出现重复的

-------------本文结束    感谢阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!