手写 实现call(), apply()

本文详细介绍了如何在JavaScript中手动实现call()和apply()方法,分别从ES6之前和之后的角度进行了讲解,并提醒在ES6之前需要注意避免在对象中存在与方法名冲突的键名,以防数据丢失。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、call()
  1. ES6之前

    Function.prototype.myCall = function (_context){
    	// _context空检测
    	_context = _context? _context: window;
    	// 获取函数参数
    	let args = [];
    	for(let i=1, len=arguments.length; i<len; i++){
    		// 避开在eval时组装参数中字符串的麻烦
    		args.push('arguments[' + i + ']');
    	}
    	// 将函数挂到_context上
    	_context.__fn = this;
    	// 执行函数
    	let result = eval('_context.__fn(' + args + ')');
    	// 删除_context内的新函数
    	delete _context.__fn;
    	return result;
    }
    
  2. ES6之后

    Function.prototype.myCall = function (_context){
    	// _context空检测
    	_context = _context? _context: window;
    	// 获取函数参数
    	let args = [...arguments].slice(1);
    	// 将函数挂到_context上
    	let fnKey = Symbol();		// Symbol是唯一的
    	_context[fnKey] = this;
    	// 执行函数
    	let result = _context[fnKey](...args);
    	// 删除_context内的新函数
    	delete _context[fnKey];
    	return result;
    }
    
二、apply()
  1. ES6之前

    Function.prototype.myApply = function(_context, _arr){
    	// _context检测
    	_context = _context? _context: window;
    	// _arr检测
    	let args = [];
    	if(!_arr) args = null;
    	else if(_arr instanceof Array === false) throw new Error('params must be array');
    	else{
    		for(let i=0, len=_arr.length; i<len; i++){
    			// 避开在eval时组装参数中字符串的麻烦
    			args.push('_arr[' + i + ']');
    		}
    	}
    	// 函数挂到_context上
    	_context.__fn = this;
    	// 函数调用
    	let result = eval('_context.__fn(' + (args? args: '') + ')');
    	// 删除_context内的新函数
    	delete _context.__fn;
    	return result;
    }
    
  2. ES6之后

    Function.prototype.myApply = function(_context, _arr){
    	// _context检测
    	_context = _context? _context: window;
    	// 函数挂到_context上
    	let fnKey = Symbol();	// Symbol是唯一的
    	_context[fnKey] = this;
    	// 函数调用
    	let result;
    	if(!_arr) result = _context[fnKey]();
    	else if(_arr instanceof Array === false) throw new Error('params must be array');
    	else result = _context[fnKey](..._arr);
    	// 删除_context内的新函数
    	delete _context[fnKey];
    	return result;
    }
    
三、注意
  1. ES6之前不能使用 Symbol 的话,最好先判断下 _contex 中是否有与即将定义的 __fn 相同的键名,有的话需要对键值进行备份以及后续的恢复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值