This is a record 前端面试的话术集锦第六篇博文——高频考点(事件机制 & 跨域 & 存储机制 & 浏览器缓存等)
and I will keep updating this blog post. ❗❗❗
1. Hand-written call, apply and bind functions
First, consider how to implement these functions from the following points:
-
If the first parameter is not passed in, the context defaults to
window
-
The pointer is changed
this
so that the new object can execute the function and accept parameters.
- Implementation
call
:
-
The first
context
is an optional parameter. If not passed, the default context iswindow
-
Next,
context
create afn
property and set the value to the function that needs to be called. -
Because
call
multiple parameters can be passed in as parameters of the calling function, the parameters need to be stripped out. -
Then call the function and delete the function on the object
Function.prototype.myCall =