javascript-省略的混乱';这';关键词

javascript-省略的混乱';这';关键词,javascript,jquery,Javascript,Jquery,有人能解释一下以下行为吗?前两个示例按预期工作,但为什么最后一个示例不工作?我想知道当我省略“this”关键字时发生了什么。在前两个例子中,我似乎可以省略它 警报你好: $(document).ready( function() { hello = 'hello'; function sayHello() { alert( this.hello ); } sayHello(

有人能解释一下以下行为吗?前两个示例按预期工作,但为什么最后一个示例不工作?我想知道当我省略“this”关键字时发生了什么。在前两个例子中,我似乎可以省略它

警报你好:

$(document).ready(
    function()
    {
        hello = 'hello';

        function sayHello()
        {
            alert( this.hello );
        }

        sayHello();
    }
);
$(document).ready(
    function()
    {
        hello = 'hello';

        function sayHello()
        {
            alert( hello );
        }

        sayHello();
    }
);
警报你好:

$(document).ready(
    function()
    {
        hello = 'hello';

        function sayHello()
        {
            alert( this.hello );
        }

        sayHello();
    }
);
$(document).ready(
    function()
    {
        hello = 'hello';

        function sayHello()
        {
            alert( hello );
        }

        sayHello();
    }
);
警报语句错误:
未捕获引用错误:未定义hello

$(document).ready(
    function()
    {
        this.hello = 'hello';

        function sayHello()
        {
            alert( hello );
        }

        sayHello();
    }
);

在第一个示例中,当您说
hello='hello'
时,您正在为变量
hello
赋值

因此,如果您
alert(hello)
它将打印变量



当你说
this.hello='hello'
时,它会为函数分配一个属性。由于函数是
$(document)
的回调,因此它正在分配
document.hello
。但是,一般来说,在第一个示例中,当您说
hello='hello'
时,它只是匿名函数的属性,您正在为变量
hello
赋值

因此,如果您
alert(hello)
它将打印变量



当你说
this.hello='hello'
时,它会为函数分配一个属性。由于函数是
$(document)
的回调,因此它正在分配
document.hello
。但是,一般来说,它只是匿名函数的属性

全局变量自动成为
窗口
对象的属性。当您在不指定任何上下文的情况下调用函数时(即作为
functionName()
而不是
something.functionName()
),默认上下文也是
窗口
对象

您的第一个函数是设置全局变量
hello
,它与
window.hello
相同。然后,
sayHello()
函数会提醒
this.hello
,这也与
窗口相同。hello
,因为
this==window

第三种情况不同,因为外部函数正由
$(document).ready()
调用。当jQuery调用事件处理程序函数时,上下文是事件的目标。在本例中,
this==document
,因此当您执行以下操作时:

this.hello = 'hello';
这相当于:

document.hello = 'hello';
这不会设置全局变量
hello
,因此
sayHello
不会访问它。这相当于:

$(文档)。准备好了吗(
函数()
{
this.hello='hello';
函数sayHello()
{
警报(document.hello);
}
你好;
}
);

全局变量自动成为
窗口
对象的属性。当您在不指定任何上下文的情况下调用函数时(即作为
functionName()
而不是
something.functionName()
),默认上下文也是
窗口
对象

您的第一个函数是设置全局变量
hello
,它与
window.hello
相同。然后,
sayHello()
函数会提醒
this.hello
,这也与
窗口相同。hello
,因为
this==window

第三种情况不同,因为外部函数正由
$(document).ready()
调用。当jQuery调用事件处理程序函数时,上下文是事件的目标。在本例中,
this==document
,因此当您执行以下操作时:

this.hello = 'hello';
这相当于:

document.hello = 'hello';
这不会设置全局变量
hello
,因此
sayHello
不会访问它。这相当于:

$(文档)。准备好了吗(
函数()
{
this.hello='hello';
函数sayHello()
{
警报(document.hello);
}
你好;
}
);

因为没有名为
hello
的局部变量,这与此不起作用的原因相同
var hello={x:123};警报(x)。我认为更有趣的是为什么第一个示例有效。@AlexeiLevenkov这些是全局的,没有
严格模式
@White OP的困惑不是关于范围,而是因为jQuery$(document)。ready函数将其设置为document,所以this.hello创建document的属性,而不是全局对象(浏览器中的aka窗口)。因为没有名为
hello
的局部变量,所以这与
var hello={x:123}不起作用的原因相同;警报(x)。我认为更有趣的是为什么第一个示例有效。@AlexeiLevenkov这些是全局的,没有
严格模式
@White OP的困惑不是关于范围,而是因为jQuery$(document)。ready函数将其设置为document,所以this.hello创建document的属性,而不是全局对象(浏览器中的aka窗口)。重要提示:在第一个示例中,它实际上是
警报(this.hello)
重要提示:在第一个示例中,它实际上是
警报(this.hello)