this對象是和執行上下文息息相關的一個對象,因此它也被稱爲執行上下文對象(context object),即激活當前執行上下文的執行上下文(an object in which context the execution context is activated)。
任何對象都可以作爲執行上下文的this值,this是執行上下文的一個屬性而不是某個變量對象的屬性。(a this value is a property of the execution context, but not a property of the variable object.)
這個特性十分重要,因爲跟變量對象不同,this值從來不會參與到標識符查詢的過程,換句話說,this值是直接從執行上下文中得到的,而不會查詢原型鏈,只有當進入執行上下文的時候,this值就已經一次確認了。
在ECMAScript中是不能給this賦值的,還是那句話,this不是變量。
在全局上下文中,this值就是指全局對象。
在函數上下文中,this的值可能是不同的,是通過調用表達式由caller提供的。
// the code of the "foo" function
// never changes, but the "this" value
// differs in every activation
function foo() {
alert(this);
}
// caller activates "foo" (callee) and
// provides "this" for the callee
foo(); // global object
foo.prototype.constructor(); // foo.prototype
var bar = {
baz: foo
};
bar.baz(); // bar
(bar.baz)(); // also bar
(bar.baz = bar.baz)(); // but here is global object
(bar.baz, bar.baz)(); // also global object
(false || bar.baz)(); // also global object
var otherFoo = bar.baz;
otherFoo(); // again global object