当前位置:网站首页>Handwritten code call, apply, bind
Handwritten code call, apply, bind
2022-06-11 09:59:00 【This is a big shot】
List of articles
One 、call and apply What is it? ?
call and apply It's all about change this The direction of . The effect is the same , It's just that the way to pass the parameters is different .
Except for the first parameter ,call You can receive a list of parameters ,apply Accept only one parameter array
Two 、 Handwriting call Code and comments ~
//1, call Simple application
// var foo={
// value:'1'
// }
// function bar(){
// console.log(this.value);
// }
// bar.call(foo)
//2, take bar Function mounted to foo On the object , Make it a foo Methods
// var foo={
// value:1,
//
// bar:function(){
// console.log(this.value);
// }
// };
// foo.bar()
//3, simulation call Method can make a function an attribute of an object , Use this function by calling the properties of the object
Function.prototype.mycall=function(context){
const fn =Symbol('fn');
context=context||window//context yes {e:3}
context.fn=this
// console.log(this);// there this yes g function , hold g Function bound to context{e:3} On
const args=[...arguments].slice('1')// Separate the following parameters
const result=context.fn(...args)
delete context.fn// Delete context.fn attribute
return result
}
function g(a,b){
console.log(a,b);
console.log(this.e);
}
g.mycall({
e:3},1,2)
3、 ... and 、 Handwriting apply Code and comments
Function.prototype.mycall=function(context){
const fn =Symbol('fn');
context=context||window//context yes {e:3}
context.fn=this
// console.log(this);// there this yes g function , hold g Function bound to context{e:3} On
const args=[...arguments].slice(1)// Separate the following parameters
// console.log(args);
const result=context.fn(args)//apply and call The difference is here ,apply What is passed is an array
delete context.fn// Delete context.fn attribute
return result
}
function g(a){
console.log(a);// The output here is also an array
console.log(this.e);
}
g.mycall({
e:3},[1,2])
3、 ... and 、 Handwriting bind Code and comments
bind It returns a function
//1, Simple application
// var bar={
// a:1
// }
// function b(){
// console.log(this.a);
// }
// b.bind(bar)()
//2, Handwriting
// Write one first 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 returns a function , The arrow function guarantees this The direction is Function
return ()=>{
return this.mycall(context)
}
}
var obj={
a:1};
function bar(){
console.log(this.a);
}
bar.mybind(obj)()
边栏推荐
猜你喜欢

基于SSM+Vue+OSS的“依伴汉服”商城设计与开发(含源码+论文+ppt+数据库)

Understanding of the keyword this in JS

穆格测试控制器的作用和应用场合有哪些

ZigBee模块无线传输星形拓扑组网结构简介

ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication

鼠标点击坐标转换生成

steamVR简介

图片规则翻页

ugui图片墙

How do online app stores of laundry chain stores do?
随机推荐
优秀测试工程师必备的4项安全测试方法!
oracle 11g rac 磁盘组有空间无法增加数据文件?
Understanding polymorphism?
外包干了四年,废了...
JS foundation -- about DOM
Beginning简约博客emlog主题模板V3
Opencv image basic operation (III) -- image feature extraction (corner detection)
What are the functions and features of EMG linear light emitter?
【torch】: 并行训练并且可以动态设置第一个gpu的batch size
深夜小酌,50道经典SQL题,真香~
Esp8266 Smartconfig
php 中使用exec显示报错
RSA签名问题
Rebuilding Oracle XdB components
New feature in ES6 -- arrow function
What are the functions and applications of Mogg test controller
Mysql比较
「INS-30131」 安装程序验证所需的初始设置失败
Q355HR钢板化学成分
EMG线性光源发射器有哪些功能和特点呢?