[js高手之路]this知多少

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

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

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

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

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

1     function show(){
2             alert( this ); //object
3         }
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 ); //abc
3         }
4         show.call('abc'); //abc
1          function show(){
2             alert( this );
3         }
4         show.call( null ); //window
5         show.call( undefined ); //window
6         show.call( '' ); //''
1         function show( a, b ){
2             alert( this + '\n' + a + ',' + b ); //abc, ghostwu, 22
3         }
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, 22
5         show.apply( null, ['ghostwu', 22] ); //window, ghostwu, 22
6         show.apply( undefined, ['ghostwu', 22] );// window, ghostwu, 22

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

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

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

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

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

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

1 <input type="button" value="点我">
2 document.querySelector("input").addEventListener("click", function(){
3 alert(this); //window
4 }.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())()); //20
13         console.log(a.fn()()); //20
14         console.log(a.fn()() == (a.fn())()); //true
15         console.log(a.fn().call(this)); // 20
16         console.log(a.fn().call(a)); // 15

 

你若是真的搞懂了this,面向对象水平也不错的话,能够来试试,个人博客中这道腾讯的面试题额:

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

相关文章
相关标签/搜索