使用“this”访问全局属性函数内的关键字

发布于 2024-11-28 10:35:18 字数 872 浏览 1 评论 0原文

我确实知道,在 javascript 中,当您在函数内使用 "this" 关键字时,"this" 将根据 Quirksmode 网站。因此,当我们有一个函数并在其中使用 "this" 时,"this" 指的是全局(窗口)对象。

我对 "this" 的工作原理有点困惑,例如在下面的代码中,"this" 应该能够解析 x因为 x 几乎是全局对象(在本例中为窗口)的属性。但在本例中,this.x 会提醒 "undefined" 而不是 x 值。

var x = "Global";

function foo(){
    alert(this.x);   //undefined     
};
foo();

然后我也尝试了其他一些事情:

function bar(){
    function foo(){
        alert(this); //[Object DOMWindow]
    };
    foo();
};

bar();

如果我的理解是正确的,那么在第二种情况下 'this' 应该引用 bar() 因为它是 的所有者>foo(),但为什么它仍然引用全局对象呢?

有人可以解释关于“this”关键字的正确理论是什么吗?

I do know that in javascript, when you use "this" keyword inside a function, then "this" would refer to the 'owner' of that function according to Quirksmode website. Therefore when we have a function and we use "this" inside it, then "this" refers to the global (window) object.

I am a little confused on how "this" works, for example in the code below, "this" then should be able to resolve x since x is pretty much a property of global object (in this case window). But this.x in this case alerts "undefined" instead of the x value.

var x = "Global";

function foo(){
    alert(this.x);   //undefined     
};
foo();

I then tried some other things too:

function bar(){
    function foo(){
        alert(this); //[Object DOMWindow]
    };
    foo();
};

bar();

If my understanding is correct, then 'this' should refer to bar() in that second case since it is the owner of foo(), but why is that it instead still refers to the global object?

Can someone explain what is the correct theory regarding "this" keyword?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪之魂 2024-12-05 10:35:18

你搞错了方向。 this 的值取决于函数的调用方式,而不是函数的定义方式。

  • 如果您调用 window.foo() 那么(在 foo 内)this 将是 window
  • 如果您调用 bar.foo()< /code> 那么 this 将是 bar (尽管您需要复制 foo 所以它是 bar 的属性首先)
  • 如果你调用 baz.bar.foo() 那么this 将是 bar (你只能通过 this 获取父对象)
  • 如果你调用 foo.call(bar) 那么 this 也将是 bar 因为 call 可以让你覆盖 this
  • 如果你调用 new foo () 那么 this 将是正在创建的新

对象默认对象是window,因此如果您只调用foo(),那么这与window.foo()相同代码>.

函数定义的范围并不重要。

You've got the wrong end of the stick. The value of this depends on how the function is called, not how it is defined.

  • If you call window.foo() then (inside foo) this will be window
  • If you call bar.foo() then this will be bar (although you need to copy foo so it is a property of bar first)
  • If you call baz.bar.foo() then this will be bar (you only ever get the parent object via this)
  • If you call foo.call(bar) then this will also be bar as call lets you override this
  • If you call new foo() then this will be the new object being created

The default object is window, so if you just call foo() then that is the same as window.foo().

It doesn't matter what scope the function is defined in.

话少心凉 2024-12-05 10:35:18

总结你的问题,你问为什么在你的第一个代码片段中,this.x未定义

var x = "Global";
function foo(){
    alert(this.x);   //undefined     
}
foo();

它根本没有意义,this value 应引用全局对象 - 如果您的代码处于严格模式,您将收到 TypeError,因为 this 本身将是 undefined -.

我认为 this.x 可能是 未定义 的唯一原因是 x 的变量声明是在函数内进行的。

检查以下两个示例: 12,这是完全相同的代码,不同之处在于第二个代码被包装在 onload 事件处理程序中,因此 x 变量没有存在于全局范围内(window.x未定义)...

Summarizing your question, you ask why in your first snippet, this.x is undefined:

var x = "Global";
function foo(){
    alert(this.x);   //undefined     
}
foo();

It doesn't make sense at all, the this value should refer to the global object -if your code were on strict mode, you would get a TypeError, since this by itself would be undefined-.

The only way I think where this.x could be undefined, is in the case that the variable declaration of x was made within a function.

Check the two following examples: 1 and 2, it's exactly the same code, the difference is that the second one, the code is wrapped in an onload event handler, so the x variable doesn't exist in the global scope (window.x is undefined)...

悲欢浪云 2024-12-05 10:35:18

是的,this始终是正在执行的函数的所有者,有关此主题的最佳答案是

var x = "Global";

function foo(){
    alert(this.x); // alerts "Global" for me    
};

/how-does-this-keyword-work-within-a-javascript-object-literal/134149#134149">关于 bar(),它是一个独立的函数,this 将绑定到“全局”对象,如上面链接的答案中所述,您的情况是 DOM窗口

Yes, this is always the owner of the function being executed and the best answer about this subject is more than you ever wanted to know about this

var x = "Global";

function foo(){
    alert(this.x); // alerts "Global" for me    
};

As for the bar(), it is a standalone function and this will be bound to the "global" object as described in the answer linked above, which is your case is the DOMWindow

未蓝澄海的烟 2024-12-05 10:35:18

如果您确实想了解这是如何工作的,请阅读ECMAscript 262 规范10.3 执行上下文 部分开始。

这是第 10.4.3 节中的内容:

10.4.3 输入功能代码

控制进入执行时执行以下步骤
函数对象 F(调用者)中包含的函数代码的上下文
提供了 thisArg,并且调用者提供了argumentsList:

  1. 如果函数代码是严格代码,则将ThisBinding设置为
    thisArg。

  2. 否则,如果 thisArg 为 null 或未定义,则将 ThisBinding 设置为
    全局对象。

  3. 否则,如果 Type(thisArg) 不是 Object,则将 ThisBinding 设置为
    ToObject(thisArg) 。

  4. 否则将 ThisBinding 设置为 thisArg。

  5. 让 localEnv 为调用 NewDeclarativeEnvironment 的结果
    传递 F 的 [[Scope]] 内部属性的值作为
    论证。

  6. 将 LexicalEnvironment 设置为 localEnv。

  7. 将 VariableEnvironment 设置为 localEnv。

  8. 令 code 为 F 的 [[Code]] 内部属性的值。

  9. 使用函数代码执行声明绑定实例化
    代码和argumentsList如10.5中所述。

If you really wish to learn how this works, then read the ECMAscript 262 specs from the section 10.3 Execution Context and onwards.

Here is what it says in section 10.4.3:

10.4.3 Entering Function Code

The following steps are performed when control enters the execution
context for function code contained in function object F, a caller
provided thisArg, and a caller provided argumentsList:

  1. If the function code is strict code, set the ThisBinding to
    thisArg.

  2. Else if thisArg is null or undefined, set the ThisBinding to the
    global object.

  3. Else if Type(thisArg) is not Object, set the ThisBinding to
    ToObject(thisArg) .

  4. Else set the ThisBinding to thisArg.

  5. Let localEnv be the result of calling NewDeclarativeEnvironment
    passing the value of the [[Scope]] internal property of F as the
    argument.

  6. Set the LexicalEnvironment to localEnv.

  7. Set the VariableEnvironment to localEnv.

  8. Let code be the value of F‘s [[Code]] internal property.

  9. Perform Declaration Binding Instantiation using the function code
    code and argumentsList as described in 10.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文