当前位置:网站首页>Implementation of call, apply and bind
Implementation of call, apply and bind
2022-07-19 16:44:00 【Maple of the sky】
call
call Method is using a specified this A function or method is called before a value and several specified parameter values
var obj = {
value:'vortestnail',
};
function fn(){
console.log(this.value);
}
fn.call(obj); //vortestnail
adopt call Methods we have achieved the following two points :
1.call change this The direction of , Point to obj
2.fn The function executes
Write by yourself call Method
Function.prototype.mycall = function(context){
// Judge the calling object
if(typeof this !== "function"){
throw new Error("Type error")
}
// First get the parameters
let args = [...arguments].slice(1);
let result = null;
// Judge context Whether to introduce , If there is no transmission, it is set to window
context = context || window;
// Set the called method to context Properties of
//this That is, the method we want to call
context.fn = this;
// Execute the method to be called
result = context.fn(...args);
delete context.fn;
// The execution result returns
return result;
}apply
We will call After the realization of ,apply It becomes very simple , They make no difference , In addition to the way of parameter transmission
Function.prototype.myApply = function(context){
if(typeof this !== "function"){
throw new Error("Type error");
}
let result = null;
context = context || window;
// Compared with the above code , We use Symbol To ensure that the attribute is unique
// That is to ensure that the user's original definition in... Will not be rewritten context Property with the same name in
const fnymbol = Symbol();
context[fnSymbol] = this;
// Implement the method to be transferred
if(argument[1]){
result = context[fnSymbol](...argument[1]);
}else{
result = context[fnSymbol]
}
delete context[fnSymbol]();
return result;
}bind
bind Returns a function
Function.prototype.myBind = function(context){
// Determine whether the calling object is a function
if(typeof this !== "function"){
throw new Error('Type error')
}
// To obtain parameters
const args = [...arguments].slice(1),
const fn = this;
return function Fn(){
return fn.apply(
this instanceof Fn ? this:context,
// The current one arguments Refer to Fn Parameters of
args.concat(...arguments)
)
}
}边栏推荐
猜你喜欢

项目报错“BeanInitializationException: com.xxxxx.xx.dao.data.Dao can‘t get a sessionFactory“

软件设计师:12-案例分析例题

向数据库表中插入中文数据报错“1366 (HY000): Incorrect string value: ‘\xE5\x90\x95\xE5‘ for column ‘name‘ at row 1“
![[unity3d] inputfield of ugui](/img/a8/3471706be6d7dc00a75694c2ae4ca7.png)
[unity3d] inputfield of ugui

想自学软件测试?无情嘲讽。

Ribbon修改默认负载均衡策略

Nacos服务部署安装

Thesis reading_ Medical NLP_ SMedBERT

梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训

QA机器人第二节——召回
随机推荐
MyCat2启动报[MYCAT-3036][ERR_INIT_CONFIG] start FileMetadataStorageManager fail
Eslint Usage Summary
OS知识点简介(二)
奥扬科技IPO被终止注册:年营收8亿 苏伟持有67.5%股权
CANoe:.vmodule文件是什么
数据湖(十三):Spark与Iceberg整合DDL操作
软件设计师:12-案例分析例题
RT thread training learning and experience (II)
MATLAB学习第四天(决策语句)
图片数据与对应标签数据互相调出
Et working mode and lt working mode of epoll
Condition judgment function of MySQL function summary
Ribbon负载均衡策略
Dijkstra序列(DAY 66)
JMeter 21 天打卡 day08
JMeter 21 day clock in Day10
Mycat2 start message [mycat-3036][err_init_config] start filemetadatastoragemanager fail
JMeter 21 天打开 day11
线段树练题.1
51world 如何修改渲染视频/窗口的大小 ?