当前位置:网站首页>JS simulation realizes the principles of call, apply, bind and new
JS simulation realizes the principles of call, apply, bind and new
2022-07-24 16:37:00 【My_ Bells】
List of articles
Achieve one call function
call() Method uses a specified this Value and one or more parameters given separately to call a function .
The grammar and function of this method apply() The method is similar to , There is only one difference , Namely call() Method accepts a list of parameters , and apply() Method accepts an array of multiple parameters .
function.call(thisArg, arg1, arg2, …)
Function.prototype.call = function (context,...args) {
let context = context ? context : window;
let args = args ? args : [];
let key = Symbol();
context[key] = this;
let res = context[key](...args);
delete context[key];
return res;
}
Achieve one apply function
apply() Method calls a given this The value of the function , And as an array ( Or something like an array object ) Provided parameters .
func.apply(thisArg, [argsArray])
Function.prototype.apply = function (context,args) {
let context = context ? context : window;
let args = args ? args : [];
let key = Symbol();
context[key] = this;
let res = context[key](...args);
delete context[key];
return res;
}
Achieve one bind function
bind() Method creates a new function , stay bind() When called , Of this new function this Is specified as bind() The first parameter of , The rest of the parameters will be the parameters of the new function , For calling .
function.bind(thisArg[, arg1[, arg2[, …]]])
Function.prototype.bind = function (context,...args) {
if(typeof this !== 'function'){
return new TypeError('must be function');
}
let _this=this;
return function F(...newArgs){
if(this instanceof F){
return new _this(...args,...newArgs);
}
return _this.apply(context,...args,...newArgs);
}
}
Realization new function
new Operator to create an instance of a user-defined object type or a built-in object with a constructor .new The keyword will do the following :
- Create an empty simple JavaScript object ( namely {});
- Link the object ( That is, set the constructor of the object ) To another object ;
- Will step 1 The newly created object serves as this The context of ;
- If the function does not return an object , Then return to this.
function create(fn,...args){
//fn Yes. new Function of ,args It's a function parameter
// Create an empty object obj, And then fn Prototype linked to obj On the prototype of
let obj = Object.create(fn.prototype);
// binding this Implementation inheritance ,obj You can access the properties in the constructor
let res = fn.apply(obj,args);
// Object returned by constructor takes precedence ,object/array/function Priority return , For other types, return obj
return res instanceof Object ? res : obj;
}
边栏推荐
猜你喜欢

thinkphp3.2.5无法跳转到外部链接

Creation and inheritance of JS class

期盼已久全平台支持-开源IM项目OpenIM之uniapp更新

Why should we launch getaverse?

15. ARM embedded system: how to debug single board with PC

如何在 PHP 中防止 XSS

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

MySQL basic commands
[email protected]"/>ZBar source code analysis - img_ scanner. c | [email protected]

Quickly view the version of redis in the server
随机推荐
Using native JS to realize magnifying glass function
Quickly view the version of redis in the server
在 PHP Web 应用程序中防止 XSS 的最佳实践
You really should go to the factory to move bricks!
MySQL basic commands
Analysis of double pointer sliding window method and solution of leetcode related problems
百度推广“删除重提”是什么意思?
MODIS 16 day data monthly / quarterly synthesis
[technology] chat room demo of uniapp
Jia Yueting's Faraday will receive another financing of US $225million in the future, and ff91 will be mass produced soon!
我们为什么要推出Getaverse?
EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
MySQL converts strings to numeric types and sorts them
Qt设计机器人仿真控制器——按键控制机器人关节转动
ArcGIS layer annotation display
Qt键盘事件(二)——长按按键反复触发event事件问题解决
Dongfang Guangyi refuted the rumor late at night, saying that the news that the hammer was filed for investigation was untrue!
Simply use MySQL index
我们为什么要推出Getaverse?
TCP protocol debugging tool tcpengine v1.3.0 tutorial