// 定義一個全局函數 function foo() { if (this === window) { console.log("this is window."); } } // 函數foo也是對象,所以可以定義foo的屬性boo為一個函數 foo.boo = function() { if (this === foo) { console.log("this is foo."); } else if (this === window) { console.log("this is window."); } }; // 等價于window.foo(); foo(); // this is window. // 可以看到函數中this的指向調用函數的對象 foo.boo(); // this is foo. // 使用apply改變函數中this的指向 foo.boo.apply(window); // this is window.