在JavaScript中這個詞是對調用對象的引用。
因此,如果我們有一個全球性的功能
function() {console.log(this);} // Window
我們拿到的全局對象或窗口。
如果我們有一個更深層次的功能在一個對象,如果我們使用的應用調用模式
obj.f.apply(window); // window is now bound to this.
在您的例子,如果你只是調用
var obj = {f: function(){console.log(this);}};
obj.f(); // Object {f: function} we get the object that wraps the function.
這條規則被打破
Person(); // in global scope
你會有效地分配
window.firstName = // ...
window.lastName = // ...
window.age = //...
因爲從全球範圍
調用時this關鍵字綁定到窗口對象基本上,你創建一個構造函數,應與新的運營商來調用。新的運營商包的功能有一個創建一個對象,並使用與對象作爲此參數的應用方法調用你的函數。
var bob = Person("bob"); // bob is undefined but you changed the window object
var sue = new Person("sue"); // sue is an object like you expect.
檢查你的代碼
var family = {}; // make an object
family.mother = new Person("Susan", "Doyle", 32);
,因爲您在函數執行JS,使一個新的對象,爲您和使用適用模式到對象之前使用新的。這就是發生了什麼
family.mother = {};
Person.apply(family.mother, ["Susan", "Doyle", 32]);
'firstName = firstName;'基本上是一個無操作。將自己的本地變量分配給自己。 – Thilo
JavaScript不是*那*神奇; 'this'是唯一引用新創建的對象的東西。 –