当前位置:网站首页>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)()
边栏推荐
- UGUI鼠标点击扩散UI效果
- 帝国CMS仿《手艺活》DIY手工制作网源码/92kaifa仿手艺活自适应手机版模板
- 对于力士乐电磁换向阀的功能与作用,你知道多少
- Override and reload?
- 鼠标点击坐标转换生成
- 【Objective-C】动态创建控件
- 面试复习之---闭包
- Suffix Array
- How much do you know about software compatibility testing? How to select a software compatibility testing organization?
- How to determine whether two time periods overlap?
猜你喜欢

ugui图片墙

Q1 revenue exceeded expectations. Why did man Bang win headwind growth?

CVPR 2021: learning continuous image representation with local implicit image function

How do we connect to WiFi?

Image quality evaluation including Matlab source code

【Objective-C】‘NSAutoreleasePool‘ is unavailable: not available in automatic reference counting mode

ESP8266_ SNTP(Simple Network Time Protocol)

ESP8266_ Connect to Alibaba cloud through mqtt protocol

图片规则翻页

整型提升例题
随机推荐
ESP8266_SmartConfig
关于马格齿轮泵的应用领域都有哪些?总结一下
【Objective-C】‘NSAutoreleasePool‘ is unavailable: not available in automatic reference counting mode
RAC单独修改scanip到不同网段时会报错
DOtween使用方法
What are the functions and functions of the EMG actuator
【bert】:在训练bert 语义相似的任务时,last ave state 的计算
What are the application fields of MAG gear pump? To sum up
Product list display
ZigBee模块无线传输星形拓扑组网结构简介
[image denoising] image denoising based on mean + median + Gauss low pass + various improved wavelet transform, including Matlab source code
【Objective-C】动态创建控件
Can station B make money?
RAC expdp export error: ora-31693, ora-31617, ora-19505
Technology cloud report: privacy computing under the wave of Web3.0
动态渲染数据和轮播图
JS foundation -- Operator
rac expdp导出时报错:ORA-31693、ORA-31617、ORA-19505
LeetCode刷题 —— 手撕二叉树
基于SSM+Vue+OSS的“依伴汉服”商城设计与开发(含源码+论文+ppt+数据库)