当前位置:网站首页>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;
}
边栏推荐
- 如何防止跨站点脚本 (XSS) 攻击完整指南
- Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
- Codeworks round 693 (Div. 3) C. long jumps problem solution
- 简易版QQ?Qt也可以实现!(一)
- Multithreading (basic)
- hping3安装使用
- How to prevent XSS in PHP
- Jenkins CLI 命令详解
- Meeting OA project progress (II)
- [LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
猜你喜欢

Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st

How to generate complex flow chart of XMIND

Mcd12q1 data shows multiple classifications in envi

Template method mode

Win10 download address

Meeting OA project progress (II)

Envi SHP to ROI and mask the grid

Why should we launch getaverse?

多线程(基础)

EF LINQ Miscellany
随机推荐
1184. Distance between bus stops
Intel plans to sell baseband chip business, but Apple has given up?
安全的证券公司有哪些?我想在手机上买股票
【机器学习基础】——另一个视角解释SVM
双亲委派机制
Codeforces round 690 (Div. 3) B. last year's substring conventional solution
MySQL basic commands
TCP protocol debugging tool tcpengine v1.3.0 tutorial
How to realize seamless rotation map? Write it again with native JS
EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
The differences of several deletions in SQL
会议OA项目进度(二)
With regard to performance testing, dry goods hit "suggestions collection"
.net review the old and know the new: [6] what is LINQ
Qt信号和槽连接失败原因及解决办法
EF combined with sqlbulkcopy batch insert data
What are the safe securities companies? I want to buy stocks on my mobile phone
剑指 Offer 22. 链表中倒数第k个节点
File browser? QT can also be achieved!
会议OA项目进度(一)