forked from WangJia-mm/JavaScript201708
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-2.js
More file actions
30 lines (26 loc) · 1.24 KB
/
2-2.js
File metadata and controls
30 lines (26 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Function.prototype.call = function call(context) {
//->this:fn[当前我们需要操作的函数,也就是Function类的实例]
//1、把函数中的THIS改为第一个参数值(理解为把this中的this关键字改变为context的值)
//2、把函数执行(把this执行)
// this();
};
*/
function fn1() {console.log(1);}
function fn2() {console.log(2);}
fn1.call(fn2);//->call执行的时候,把fn1中的this变为fn2,让fn1执行 =>1
fn1.call.call.call.call(fn2);//->call执行的时候,把“fn1.call.call.call” 中的this变为fn2,然后让“fn1.call.call.call”执行
//-> fn1.call.call.call
// = function call() { [native code] } 就是call这个方法
//-> 把call执行,让call中的this变为fn2,根据call的实现源码分析得到,我们其实就是让fn2在执行
//-> 2
Function.prototype.call(fn1);//->Function.prototype是一个匿名函数(anonymous),它是函数数据类型的,不是对象类型的(但是操作起来和其它类的原型一模一样)
Function.prototype.call.call.call.call(fn1);
//->Function.prototype.call.call.call
//-> =
//->function call(){[native code]}
// function call(){[native code]}.call(fn1)
//->function call(){[native code]} =>A
//-> A.call(fn1)
//-> A中的this:fn1
//-> A执行