本文介绍了用'this'定义的函数,但没有'this'执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待第二次调用taco函数来生成运行时错误,因为我没有使用this关键字调用它:

I was expecting the 2nd call of the "taco" function to generate a runtime error since I am not calling it with the "this" keyword:

function foo() {
    var bar = "baz";

    this.taco = function() {
        console.log(bar);
    };
    this.taco();
    taco(); // I expected a runtime error here.     
}

foo();

但是,它没有。

以下是相同代码的小提琴:

Here is a fiddle of the same code: http://jsfiddle.net/phillipkregg/gdFxU/226/

JavaScript是否在此处使用某种类型的隐式上下文管理?

Is JavaScript using some type of implicit context management here?

好奇,谢谢!

推荐答案

原因是当你打电话给 foo(),您在窗口对象的范围内调用它。这意味着在 foo()中,这个的值设置为 window

The reason is that when you call foo(), you are invoking it in the scope of the window object. That means that inside of foo(), the value of this is set to window.

因此, this.taco()实际上是 window.taco() taco()相同。换句话说, taco()是一个全局函数,所以无论你将它称为 taco(), as window.taco() this.taco()窗口

Thus, this.taco() is actually window.taco() which is the same as taco(). In other words, taco() is a global function so it works either way you call it as taco(), as window.taco() or as this.taco() when this is window.

如果涉及 taco()作为这样的新对象,其中设置为 foo 的新实例,并且不相等到窗口,然后你得到预期的运行时错误:

If you involve taco() as a new object like this where this is set to a new instance of foo and is not equal to window, then you get the expected run-time error:

function foo() {
    var bar = "baz";

    this.taco = function() {
        console.log(this);
        console.log(bar);
    };
    this.taco();
    taco(); // I expected a runtime error here.     
}

var x = new foo();

示例:

如果你是混淆了这个的价值,有这些javascript规则确定这个的价值:

If you are confused about the value of this, there are these javascript rules that determine the value of this:


  1. 如果您使用 new 调用函数,例如 x = new foo(),然后创建一个新的 foo 实例,其值 this 设置为 foo()函数内的该对象,默认情况下从 foo()返回新实例。

  1. If you call a function with new like x = new foo(), then a new instance of foo is created and the value of this is set to that object inside the foo() function and that new instance is returned from foo() by default.

如果您通常调用任何函数,例如 foo(),那么<$ c $的值c>此设置为浏览器中的全局对象 window 或者如果是在javascript的较新严格模式中,则将是 undefined 。这就是原始示例中发生的情况。

If you call any function normally like foo(), then the value of this is set to be the global object which in a browser is window or if in javascript's newer "strict" mode, then this will be undefined. This is what was happening in your original example.

如果使用对象引用调用方法,如 obj.foo(),然后将被设置为 obj 对象。

If you call a method with an object reference like obj.foo(), then this will be set to be the obj object.

如果使用 .apply() .call(),那么你可以通过第一个参数 .apply()这个的值是多少$ c>或 .call()。例如: foo.call(obj)将调用 foo()函数并设置这个指向 obj 对象的指针。

If you make a function call with .apply() or .call(), then you can specifically control what the value of this is set to with the first argument to .apply() or .call(). For example: foo.call(obj) would call the foo() function and set the this pointer to the obj object.

如果你不是在任何函数调用中(例如在全局范围内),然后这个将是全局对象(窗口在一个浏览器)或未定义在严格模式下。

If you are not in any function call (e.g. at the global scope), then this will be either the Global object (window in a browser) or undefined in strict mode.

与上述所有规则一样,这个由调用者如何调用你来控制,而不是如何定义函数/方法。

As in all of the above rules, this is controlled by how the caller calls you, not how the function/method is defined.

这篇关于用'this'定义的函数,但没有'this'执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:59