当前位置:网站首页>JS interview question: FN call. call. call. Call (FN2) parsing
JS interview question: FN call. call. call. Call (FN2) parsing
2022-04-23 17:40:00 【Lost Camel】
There is such a JS Interview questions , ask fn.call.call.call.call(fn2) What is the result ?
First say call Internal principle of function :
- You can change the value of the function this;
- Execute function
Let's simulate Native call function :
Function.prototype.call = function(context, ...args){
context = context? Object(context) : window;
// change this
context.fn = this;
// Execute function
return context.fn(...args);
}
as follows :
function fn(){
console.log(11)
}
function fn2(){
console.log(22)
}
ask :
fn.call.call.call.call(fn2) What is the result ?
analysis :
- The front string
fn.call.call.call.callNo call , Just get the of the object call attribute , therefore , The result of this string is Function.call attribute . - So that string Namely
Function.call.call(fn2); It can also be cleaved into fn3.call(fn2). - according to call Principle ( Please refer to the above call simulation ), stay fn3 perform call, It is actually implemented in this way
fn2.fn3(). - because fn3 It's actually call function , therefore , fn2.fn3() Equivalent to
fn2.call(). - therefore , The final result of the above string of code , It's called fn2, So the result output 22.
版权声明
本文为[Lost Camel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230552420727.html
边栏推荐
- Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
- HCIP第五次实验
- 402. 移掉 K 位数字-贪心
- 48. Rotate image
- tidb-server 的配置文件在哪里?
- Shell - introduction, variables, and basic syntax
- In embedded system, must the program code in flash be moved to ram to run?
- 48. 旋转图像
- Allowed latency and side output
- Halo open source project learning (II): entity classes and data tables
猜你喜欢

JVM class loading mechanism

HCIP第五次实验

PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet

Dry goods | how to extract thumbnails quickly?

2021 Great Wall Cup WP

For the space occupation of the software, please refer to the installation directory

2. Electron's HelloWorld
MySQL installation

Learning record of uni app dark horse yougou project (Part 2)

958. 二叉树的完全性检验
随机推荐
双指针进阶--leetcode题目--盛最多水的容器
一些问题一些问题一些问题一些问题
[difference between Oracle and MySQL]
Self use learning notes - connected and non connected access to database
How to manually implement the mechanism of triggering garbage collection in node
Summary of common SQL statements
[ES6] promise related (event loop, macro / micro task, promise, await / await)
[WPF binding 3] listview basic binding and data template binding
为什么有些人说单片机简单,我学起来这么吃力?
394. String decoding - auxiliary stack
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
For the space occupation of the software, please refer to the installation directory
958. 二叉树的完全性检验
Model problems of stock in and stock out and inventory system
402. 移掉 K 位数字-贪心
[binary number] maximum depth of binary tree + maximum depth of n-ary tree
Tdan over half
386. Dictionary order (medium) - iteration - full arrangement
Node template engine (EJS, art template)
ASP. Net core configuration options (Part 1)