手写call函数

134 阅读1分钟
var name = '123';
var obj = {
	name: '456'
}
Function.prototype.myCall = function(context) {
	if (context === undefined || context === null) {
		context = window;
	}
	
	context.fn = this;
	const argu = [...arguments].slice(1);
	const result = context.fn(...argu);
	delete context.fn;
	return result;
}
function tc() {
	console.log(this.name)
}
tc()
tc.call(obj)
tc.myCall(obj)