我注意到一些东西在玩耍时引发了一个快速问题。Javascript:全局上下文和函数上下文声明
当在全局/窗口上下文中执行代码时,将任何函数声明作为方法添加到窗口对象中。
但是,当我在另一个对象的上下文中,编写一个函数声明不会将该方法添加到我的对象方法。
function functionInGlobalCtx() { // This will be added as a function to the window object
// code...
}
var myObject = {};
myObject.myObjectFunction = function() {
var $this = this; // The context here is the 'myObject' object
function functionHopefullyInMyObjectCtx() {
// code...
}
}
myObject.myObjectFunction();
为什么函数声明作为窗口对象的一部分而不是对象的一部分存在?
这是简单的'如何JavaScript的作品'(特殊规则适用于全球上下文吗?)还是我错过了什么?
谢谢。