当前位置:网站首页>Manually implement call, apply and bind functions
Manually implement call, apply and bind functions
2022-04-23 17:14:00 【MiMenge】
Realization call
Function.prototype.myCall = function (targetObj, ...resule) {
// Judge whether the incoming object exists
targetObj = targetObj || window;
// Hang on to the incoming object this Point to , here this To call myCall function
targetObj.fn = this;
// Call... Internally using an incoming object fn Method , This enables changes this Point to , Point to the target object that is passed in
let value = eval('targetObj.fn(...resule)');
// Delete target object fn Method
delete targetObj.fn
// Returns the result of the call
return value;
}
let obj = {
uname: 'Tom'
}
function demo(a, b){
console.log(this.uname, a, b);
}
demo();// undefined undefined undefined
demo.myCall(obj, 1, 2);
// Tom 1 2
Realization apply ( Subject and call almost , Different ways of passing in parameters )
Function.prototype.myApply = function (targetObj, resule) {
targetObj = targetObj || window;
targetObj.fn = this;
let value = eval('targetObj.fn(...resule)');
delete targetObj.fn
return value;
}
let obj = {
uname: 'Tom'
}
function demo(x, y){
console.log(this.uname, x, y);
}
demo();// undefined undefined undefined
demo.myApply(obj, [1, 2]);
// Tom 1 2
Realization bind
Function.prototype.myBind = function (targetObj, ...resule) {
// Determine whether the called object is a function
if (typeof this !== "function") {
throw new Error("this must be a function");
}
// Create variables to save this Point to
// here this Point to the function called externally
let self = this;
// The function to be created returns
let backFunc = function () {
// First determine the of the calling object this Whether it is the same as this function this
// Yes, change this Point to yourself ,( In fact, the last thing to return is this A function that points to itself )
// If not, change the caller this Point to the incoming object , Pass the parameter to the calling this in .
self.apply(this instanceof self ?
this : targetObj, resule.concat(Array.prototype.slice.call(arguments)));
}
// Judge whether the call has a prototype
if(this.prototype) {
// If so, assign the prototype to the returned function
backFunc.prototype = Object.create(this.prototype);
}
// Return function
return backFunc;
}
let obj = {
uname: 'Tom'
}
function demo(x, y){
console.log(this.uname, x, y);
}
demo();// undefined undefined undefined
demo.myBind(obj, 1, 2)();
// Tom 1 2
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553027792.html
边栏推荐
- freeCodeCamp----prob_ Calculator exercise
- .Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
- VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
- C listens for WMI events
- 快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
- Kingdee Cloud Star API calling practice
- Rtklib 2.4.3 source code Notes
- Nodejs installation and environment configuration
- [registration] tf54: engineer growth map and excellent R & D organization building
- Expression "func" tSource, object "to expression" func "tSource, object" []
猜你喜欢
[WPF binding 3] listview basic binding and data template binding
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Shell script -- shell programming specification and variables
RPC核心概念理解
.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)
Using quartz under. Net core - [1] quick start
2. Electron's HelloWorld
文件操作《二》(5000字总结篇)
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
Milvus 2.0 détails du système d'assurance de la qualité
随机推荐
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Webapi + form form upload file
Self use learning notes - connected and non connected access to database
Using quartz under. Net core - [1] quick start
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
C语言函数详解
El cascade and El select click elsewhere to make the drop-down box disappear
ASP. Net core dependency injection service life cycle
Abnormal resolution of Xiaomi camera
Clickhouse table engine
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
Promise (I)
freeCodeCamp----shape_ Calculator exercise
线性代数感悟之1
[problem solving] [show2012] random tree
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
Clickhouse SQL operation
[registration] tf54: engineer growth map and excellent R & D organization building
Redis docker installation