this 的绑定规则 1. 默认绑定 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function foo ( ) { console .log (this ); } foo ();const obj = { name : "aaa" , foo ( ) { console .log (this ); }, }; obj.foo ();
2. 隐式绑定 1 2 3 4 5 6 7 8 function foo ( ) { console .log (this ); } const obj = { bar : foo, }; obj.bar ();
3. 显示绑定 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 31 32 const obj = { name : "zzz" , }; function foo ( ) { console .log (this ); } foo.call (obj); foo.call (123 ); foo.call ("aaa" ); foo.call (undefined ); foo.call (null ); function bar (name, age ) { console .log (this ); console .log (name, age); } bar.call (obj, "aaa" , 18 ); bar.apply (obj, ["aaa" , 18 ]); const foo1 = foo.bind (obj);foo1 (); foo1 (); foo1 ();
4. new 绑定 1 2 3 4 5 6 7 8 9 10 11 function foo ( ) { console .log (this ); } new foo ();
5. this 绑定的优先级 ==new 绑定 > 显示绑定 > 隐式绑定 > 默认绑定 ==
==在显示绑定中 bind 的优先级高于 apply 和 call==
6. 箭头函数中的 this 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 const foo = ( ) => { console .log (this ); }; const obj = { bar : foo, getData ( ) { setTimeout (() => { console .log (this ); }, 0 ); }, }; foo (); foo.call ({ count : 1 }); obj.bar (); obj.getData ();
this 练习题 1. 题一 1 2 3 4 5 6 7 8 9 10 11 12 13 var name = "window" ;var person = { name : "person" , sayName : function ( ) { console .log (this .name ); }, }; function sayName ( ) { var sss = person.sayName ; sss (); person.sayName ()(person.sayName )(); } sayName ();
2. 题二 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 31 32 33 34 var name = "window" ;var person1 = { name : "person1" , foo1 : function ( ) { console .log (this .name ); }, foo2 : () => console .log (this .name ), foo3 : function ( ) { return function ( ) { console .log (this .name ); }; }, foo4 : function ( ) { return () => { console .log (this .name ); }; }, }; var person2 = { name : "person2" };person1.foo1 (); person1.foo1 .call (person2); person1.foo2 (); person1.foo2 .call (person2); person1.foo3 ()(); person1.foo3 .call (person2)(); person1.foo3 ().call (person2); person1.foo4 ()(); person1.foo4 .call (person2)(); person1.foo4 ().call (person2);
3. 题三 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 31 32 33 34 var name = "window" ;function Person (name ) { this .name = name; (this .foo1 = function ( ) { console .log (this .name ); }), (this .foo2 = () => console .log (this .name )), (this .foo3 = function ( ) { return function ( ) { console .log (this .name ); }; }), (this .foo4 = function ( ) { return () => { console .log (this .name ); }; }); } var person1 = new Person ("person1" );var person2 = new Person ("person2" );person1.foo1 (); person1.foo1 .call (person2); person1.foo2 (); person1.foo2 .call (person2); person1.foo3 ()(); person1.foo3 .call (person2)(); person1.foo3 ().call (person2); person1.foo4 ()(); person1.foo4 .call (person2)(); person1.foo4 ().call (person2);
4. 题四 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 var name = "window" ;function Person (name ) { this .name = name; this .obj = { name : "obj" , foo1 : function ( ) { return function ( ) { console .log (this .name ); }; }, foo2 : function ( ) { return () => { console .log (this .name ); }; }, }; } var person1 = new Person ("person1" );var person2 = new Person ("person2" );person1.obj .foo1 ()(); person1.obj .foo1 .call (person2)(); person1.obj .foo1 ().call (person2); person1.obj .foo2 ()(); person1.obj .foo2 .call (person2)(); person1.obj .foo2 ().call (person2);