詳解js和jquery裏的this關鍵字

js中的this

我們要記住:this永遠指向函數運行時所在的對象!而不是函數被創建時所在的對象。
this對象是在運行時基於函數的執行環境綁定的,在全局環境中,this等於window

先來看個例子:

<script>
   var fullname = "Trigkit4";   var person = {
       fullname : 'Jack',
   prop:{
       fullname : 'Blizzard',
       getFullname : function () {           return this.fullname;
       }
   }
};console.log(person.prop.getFullname());//Blizzardvar test = person.prop.getFullname;console.log(test());//Trigkit4</script>

getFullname被分配到test變量時,上下文指的是全局對象(window)。這是因爲test是被隱式設置爲全局對象的屬性。出於這個原因,該函數返回windowfullname,所以在這裏 this 指的是window, 所以返回的是第一個fullname

說明

this 關鍵字通常在對象的 構造函數中使用,用來引用對象。

關鍵字this:總是指向調用該方法的對象,如:

var iCar = new Object();
iCar.color = "red";
iCar.showColor = function(){
     alert(this.color);//輸出"red"};

關鍵字this用在對象的showColor()方法中,在此環境,this等於iCar

使用this是因爲在實例化對象時,總是不能確定開發者會使用什麼樣的變量名。使用this,即可在任意多個地方重用同一個函數。考慮下面的例子:

function showColor(){
     alert(this.color);
}var oCar1 = new Object;
oCar1.color = "red";
oCar1.showColor = showColor;var oCar2 = new Object;
oCar2.color = "blue";
oCar2.showcolor = showcolor;

oCar1.showColor();//輸出"red"oCar2.showColor();//輸出"blue"

這段代碼中,首先用this定義函數showColor(),然後創建兩個對象oCar1oCar2,一個對象屬性被設置爲"red",另一個爲blue;兩個對象都被賦予了屬性showColor,指向原始的showColor()函數,調用每個showColor的方法,oCar1輸出redoCar2輸出blue

引用對象屬性時,必須使用this關鍵字。

所有基於全局作用域的變量其實都是window對象的屬性(property)。這意味着即使是在全局上下文中,this變量也能指向一個對象。

對於 JScript 的客戶版本,如果在其他所有對象的上下文之外使用 this,則它指的是 window 對象。
例如:

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
    <script type="text/javascript"> 
        alert(this);//彈出 object window;
    </script></head><body></body>

作爲構造函數調用

所謂構造函數,就是通過這個函數生成一個新對象(object)。這時,this就指這個新對象。

<script type="text/javascript"> 
        function test(){            this.x = 10;
        }        var obj = new test();
        alert(obj.x); //彈出 10;
 </script>

全局環境中的this

在看下面一個this出現在全局環境中的例子:

<script type="text/javascript"> 
    var name = "全局";    function getName(){        var name = "局部";        return this.name;
    };
    alert(getName());//彈出 全局; </script>

函數getName()所處的對象是window對象,因此this也一定在window對象中。此時的this指向window對象,所以getName()返回的this.name其實是window.name,因此alert全局

結論:無論this身處何處,一定要找到函數運行時(或者說在何處被調用 了)的位置。

通過不同的調用語法,改變相同函數內部this的值:

<script type="text/javascript">
    var foo = {
        test:function(){
            alert(this);
        }
    }
    foo.test();//object,因爲test方法在調用時屬於foo對象

    var baz = foo.test;
    baz();//window,因爲baz()被調用時屬於global對象</script>

局部環境中的this

看下面一個this出現在局部環境中的例子

<script type="text/javascript"> 
    var name = "全局";    var jubu={
    name:"局部",
    getName:function(){         return this.name;
     }
    };
    alert(jubu.getName());</script>

其中this身處的函數getName不是在全局環境中,而是處在jubu環境中。無論this身處何處,一定要找到函數運行時的位置。此時函數getName運行時的位置:

alert(jubu.getName());

顯然,函數getName所在的對象是jubu,因此this的安身之處定然在jubu,即指向jubu對象,則getName返回的this.name其實是jubu.name,因此alert出來的是“局部”!

作用域鏈中的this

<script type="text/javascript">function scoping () {  console.log(this);  return function () {    console.log(this);
  };
}

scoping()();
>>window>> window</script>

因爲scoping函數屬於window對象,自然作用域鏈中的函數也屬於window對象。

對象中的this

可以在對象的任何方法中使用this來訪問該對象的屬性。這與用new得到的實例是不一樣的。

var obj = {
    foo: "test",
    bar: function () {        console.log(this.foo);
    }
};

obj.bar(); // "test"

重寫this

無法重寫this,因爲它是一個關鍵字。

function test () {    var this = {};  // Uncaught SyntaxError: Unexpected token this }

jquery中的this

$()生成的是什麼呢?實際上$()=jquery(),那麼也就是說返回的是一個jquery的對象。
$(this)jquery對象,能調用jquery的方法,例如click()keyup()

 $(function () {
   $('button').click(function () {
       alert(this);//this 表示原生的DOM
       //$(this)表示當前對象,這裏指的是button
   }) 
});

結論:
this,表示當前的上下文對象是一個html DOM對象,可以調用html對象所擁有的屬性,方法。
$(this),代表的上下文對象是一個jquery的上下文對象,可以調用jquery的方法和屬性值。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章