当前位置:网站首页>手写代码call,apply,bind
手写代码call,apply,bind
2022-06-11 09:51:00 【这是一个大人物】
一、call和apply是什么?
call 和 apply 都是为了解决改变 this 的指向。作用都是相同的,只是传参的方式不同。
除了第一个参数外,call 可以接收一个参数列表,apply 只接受一个参数数组
二、手写call代码以及注释~
//1, call简单应用
// var foo={
// value:'1'
// }
// function bar(){
// console.log(this.value);
// }
// bar.call(foo)
//2,将bar函数挂载到foo对象上,使其成为foo的方法
// var foo={
// value:1,
//
// bar:function(){
// console.log(this.value);
// }
// };
// foo.bar()
//3,模拟call方法可以使函数成为对象的属性,通过调用对象的属性来用该函数
Function.prototype.mycall=function(context){
const fn =Symbol('fn');
context=context||window//context是{e:3}
context.fn=this
// console.log(this);//这里的this是g函数,把g函数绑定到context{e:3}上
const args=[...arguments].slice('1')//把后面的参数分离出来
const result=context.fn(...args)
delete context.fn//删除context.fn属性
return result
}
function g(a,b){
console.log(a,b);
console.log(this.e);
}
g.mycall({
e:3},1,2)
三、手写apply代码以及注释
Function.prototype.mycall=function(context){
const fn =Symbol('fn');
context=context||window//context是{e:3}
context.fn=this
// console.log(this);//这里的this是g函数,把g函数绑定到context{e:3}上
const args=[...arguments].slice(1)//把后面的参数分离出来
// console.log(args);
const result=context.fn(args)//apply和call的区别在这,apply传过来的是一个数组
delete context.fn//删除context.fn属性
return result
}
function g(a){
console.log(a);//这里输出的也是一个数组
console.log(this.e);
}
g.mycall({
e:3},[1,2])
三、手写bind代码以及注释
bind会返回一个函数
//1,简单应用
// var bar={
// a:1
// }
// function b(){
// console.log(this.a);
// }
// b.bind(bar)()
//2,手写
//先手写一个call
Function.prototype.mycall=function(context){
const fn=Symbol('fn')
context=context||window
context.fn=this
const args=[...arguments].slice(1)
const result=context.fn(...args)
delete context.fn
return result
}
Function.prototype.mybind=function (context){
//这里返回一个函数,箭头函数保证this的指向是Function
return ()=>{
return this.mycall(context)
}
}
var obj={
a:1};
function bar(){
console.log(this.a);
}
bar.mybind(obj)()
边栏推荐
- ORACLE DG物理备库使用别名数据文件改变路径到OMF路径
- 等待事件 enq: KO - fast object checkpoint可行的一些处理方法
- Opencv image basic operation (III) -- image feature extraction (corner detection)
- php 中使用exec显示报错
- 全局池化–Pytorch
- GDB debugging common commands
- JS foundation -- about DOM
- 【bert】:在训练bert 语义相似的任务时,last ave state 的计算
- Ora-00059 exceeds DB_ Files limit
- mysql基础学习笔记03
猜你喜欢

Ora-00059 exceeds DB_ Files limit

The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)

Standard dual airbags, Changan Lumin listed, starting at 48900 yuan

JS foundation -- about DOM

Simulation of map and set

ESP8266_通过MQTT协议连接阿里云

Simple recommendation based on Euclidean distance

ESP8266_ Get request weather forecast and JSON parsing

Servlet 的初次部署

How much do you know about the functions and functions of Rexroth solenoid directional valve
随机推荐
【Objective-C】‘NSAutoreleasePool‘ is unavailable: not available in automatic reference counting mode
Opencv image basic operation (IV) -- image feature extraction (corner detection)
帝国CMS仿《游戏鸟》模板源码/92kaifa版大型手机游戏门户网站模板
EMG线性光源发射器有哪些功能和特点呢?
ESP8266_ Mqtt protocol
An error will be reported when the RAC modifies the scanip to different network segments
UGUI
Redis transaction details
What are the functions and features of EMG linear light emitter?
VK2C22A/B抗干扰系列电表,水表段码LCD液晶驱动芯片资料(可定制DICE/COG)
「INS-30131」 安装程序验证所需的初始设置失败
js基础--Array对象
steamVR简介
转载:LinearLayout+Fragment实现下导航栏效果
Esp8266 Smartconfig
Integer lifting example
What hydraulic oil is used for Denison hydraulic pump? What are the requirements
你对软件兼容性测试知道多少?如何选择软件兼容性测试机构?
Q1's revenue exceeded Wall Street's expectations, and the value of Zhiwen group is waiting to return
Interview questions: REM layout, flex layout, etc