[js高手之路]this知多少

来源:互联网 发布:java百度课程 编辑:程序博客网 时间:2024/05/17 22:38

this关键字在javascript中的变化非常的灵活,如果用的不好就非常恶心,用的好,程序就非常的优雅,灵活,飘逸.所以掌握this的用法,是每一个前端工程师必知必会的.而且这个也是一些大公司笔试中常见的考察项.

第一种、单独的this,指向的是window这个对象

console.log( this ); //window
注:当前的执行环境是window, 所以this指向了window
 
第二种、全局函数中的this
 
1         function show(){2             alert( this ); //window3         }4         show();

show()这样子调用,指向的是window

第三种、函数调用的时候,前面加上new关键字,也就是构造函数的调用方式

1     function show(){2             alert( this ); //object3         }4         new show();

new show这样调用,函数中的this指向的是object

第四种、用call与apply的方式调用函数,这里面的this分两种情况

  • 一般情况下,call与apply后面的第一个参数, 该参数是什么, this就指向什么
  • call与apply后面如果是undefined和null,this指向的是window
1         function show(){2             alert( this ); //abc3         }4         show.call('abc'); //abc
1          function show(){2             alert( this );3         }4         show.call( null ); //window5         show.call( undefined ); //window6         show.call( '' ); //''
1         function show( a, b ){2             alert( this + '\n' + a + ',' + b ); //abc, ghostwu, 223         }4         show.call( "abc", 'ghostwu', 22 );5         show.apply( "abc", ['ghostwu', 22] );
1 function show( a, b ){2             alert( this + '\n' + a + ',' + b );3         }4         show.call( "abc", 'ghostwu', 22 ); //abc, ghostwu, 225         show.apply( null, ['ghostwu', 22] ); //window, ghostwu, 226         show.apply( undefined, ['ghostwu', 22] );// window, ghostwu, 22

这里要稍微注意一下, call与apply后面的参数传递的区别: call是一个个把参数传递给函数的参数,而apply是把参数当做数组传递给函数的参数,数组第一项传递给函数的第一个参数,第二项传递给函数的第二个参数。。。以此类推

第五种、定时器中的this,指向的是window

1      setTimeout( function(){2             alert( this ); //window3         }, 500 );

第六种、元素绑定事件,事件触发后 执行的函数中的this 指向的是当前的元素

1 <input type="button" value="点我">2 <script>3 document.querySelector("input").onclick = function(){4 alert(this); //指向当前按钮5 };6 </script>

第七种、函数调用时如果绑定了bind, 那么函数中的this就指向了bind中绑定的东西

1 <input type="button" value="点我">2 document.querySelector("input").addEventListener("click", function(){3 alert(this); //window4 }.bind(window));

如果没有通过bind改变this,那么this的指向就会跟第六种情况一样

第八种、对象中的方法:该方法被哪个对象调用,那么方法中的this就指向该对象

        var obj = {            userName : "ghostwu",            show : function(){                return this.userName;            }        };        alert( obj.show() ); //ghostwu

如果把对象的方法,赋给一个全局变量,然后再调用,那么this指向的就是window.

1         var obj = {2             userName : "ghostwu",3             show : function(){4                 return this.userName;5             }6         };7         var fn = obj.show;8         var userName = 'hello';9         alert( fn() );// hello, this指向window

学完之后,我们就来应用下,下面这道题是腾讯考察this的面试题,你都能做出来吗?

 1         var x = 20; 2         var a = { 3             x: 15, 4             fn: function () { 5                 var x = 30; 6                 return function () { 7                     return this.x; 8                 }; 9             }10         };11         console.log(a.fn()); //function(){return this.x}12         console.log((a.fn())()); //2013         console.log(a.fn()()); //2014         console.log(a.fn()() == (a.fn())()); //true15         console.log(a.fn().call(this)); // 2016         console.log(a.fn().call(a)); // 15

 

你如果真的搞懂了this,面向对象水平也不错的话,可以来试试,我的博客中这道腾讯的面试题额:

学生问的一道javascript面试题[来自腾讯]

 

原创粉丝点击