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
是被隱式設置爲全局對象的屬性。出於這個原因,該函數返回window
的fullname
,所以在這裏 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()
,然後創建兩個對象oCar1
和oCar2
,一個對象屬性被設置爲"red
",另一個爲blue
;兩個對象都被賦予了屬性showColor
,指向原始的showColor()
函數,調用每個showColor
的方法,oCar1
輸出red
,oCar2
輸出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
的方法和屬性值。